Python 打印当前运行环境、获取指定文件夹下内存大小

简介: 打印当前运行环境如果你想利用 Python 脚本快速查看当前电脑的系统(Linux、Windows)、架构(32位还是 64 位)、处理器、Python 版本及运行环境等信息,下面这个代码块能够帮到你

打印当前运行环境

如果你想利用 Python 脚本快速查看当前电脑的系统(Linux、Windows)、架构(32位还是 64 位)、处理器、Python 版本及运行环境等信息,下面这个代码块能够帮到你

import platform as pl
profile = [
    'architecture',
    'machine',
    'node',
    'platform',
    'processor',
    'python_build',
    'python_compiler',
    'python_version',
    'release',
    'system',
    'version',
]
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
for key in profile:
    if hasattr(pl, key):
        print(key + bcolors.BOLD + ": " + str(getattr(pl, key)()) + bcolors.ENDC)



获取指定文件夹下内存大小

日常工作中这个模块我们可能用不到,查看文件大小的话用代码跑还不如直接鼠标右键查看该文件的属性信息;但是,对于以后开发工作中,可以将此功能镶嵌到开发的软件中,作为一个 监控文件夹内存大小 的功能存在

import os
import sys  # Load the library module and the sys module for the argument vector'''
try:
    directory = "H:/" # Set the variable directory to be the argument supplied by user.
except IndexError:
    sys.exit("Must provide an argument.")
dir_size = 0  # Set the size to 0
fsizedicr = {'Bytes': 1,
             'KB': float(1) / 1024,
             'MB': float(1) / (1024 * 1024),
             'GB': float(1) / (1024 * 1024 * 1024)}
for (path, dirs, files) in os.walk(
        directory):
    for file in files:  # Get all the files
        filename = os.path.join(path, file)
        dir_size += os.path.getsize(filename)  # Add the size of each file in the root dir to get the total size.
fsizeList = [str(round(fsizedicr[key] * dir_size, 2)) + " " + key for key in fsizedicr]  # List of units
if dir_size == 0:
    print("File Empty")  # Sanity check to eliminate corner-case of empty file.
else:
    for units in sorted(fsizeList)[::-1]:  # Reverse sort list of units so smallest magnitude units print first.
        print("{} Folder Size: ".format(directory)+ units)


相关文章
|
12天前
|
机器学习/深度学习 数据采集 TensorFlow
使用Python实现深度学习模型:智能垃圾分类与环境保护
使用Python实现深度学习模型:智能垃圾分类与环境保护 【8月更文挑战第7天】
28 2
|
2天前
|
机器学习/深度学习 Ubuntu 数据挖掘
Ubuntu系统部署Anaconda环境及Python语言的详细流程
以上就是在Ubuntu系统中安装Anaconda环境及Python语言的详细流程。Anaconda为Python科学计算提供了便捷的管理方式,帮助用户轻松处理不同项目之间依赖管理的复杂性。通过以上步骤,你现在应该有了一个完全可用的Anaconda环境,可以开始在Ubuntu上进行Python编程和数据科学项目的探索了。
13 5
|
6天前
|
IDE Linux 开发工具
如何安装Python环境?
【8月更文挑战第13天】如何安装Python环境?
17 3
|
7天前
|
机器学习/深度学习 数据采集 TensorFlow
使用Python实现深度学习模型:智能环境监测与预警
【8月更文挑战第11天】 使用Python实现深度学习模型:智能环境监测与预警
26 2
|
10天前
|
开发工具 Python Windows
【Python专栏】搭建Pyhthon运行环境及开发环境 | 安装Python | 安装PyCharm
【Python专栏】搭建Pyhthon运行环境及开发环境 | 安装Python | 安装PyCharm
40 4
|
1月前
|
数据采集 存储 API
Python虚拟环境数据共享技术解析:最佳实践与常见误区
本文探讨了Python爬虫开发中如何在虚拟环境中管理数据,提倡使用共享目录、数据库和API进行数据共享。通过创建虚拟环境、安装依赖并提供一个使用代理IP爬取微博数据的示例,阐述了如何配置代理、解析网页及保存数据到共享路径。强调了避免硬编码路径、忽视依赖管理和数据安全性的误区。
51 11
Python虚拟环境数据共享技术解析:最佳实践与常见误区
|
14天前
|
Ubuntu Linux iOS开发
如何实现多个Python环境的Python版本切换
【8月更文挑战第4天】如何实现多个Python环境的Python版本切换
56 5
|
1月前
|
缓存 自然语言处理 开发工具
Python中 __pycache__文件夹是什么?
Python中 __pycache__文件夹是什么?
23 5
|
16天前
|
Linux 开发工具 Python
【Deepin 20系统】Linux系统从零打造完美VScode for Python环境
如何在Deepin 20系统中从零开始配置一个完美的VScode for Python开发环境,包括安装Anaconda、VScode、必要的插件、汉化、主题和字体设置,以及如何运行和调试Python程序。
28 5