打印当前运行环境
如果你想利用 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)