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)


相关文章
|
2天前
|
项目管理 Python
如何在Mac上安装多个Python环境
在你的Mac上使用多个Python环境可以对项目管理很有帮助,特别是在同时处理不同Python版本或不同的包需求时。在这篇文章中,我们将向你展示如何在Mac上轻松地安装和管理多个Python环境。
15 5
 如何在Mac上安装多个Python环境
|
17天前
|
Python
Python的Virtualenv与Venv环境管理器
介绍Python的两种环境管理工具Virtualenv和venv,包括它们的安装、创建、激活、退出环境以及查看帮助信息的方法,同时对比了两者的特点和使用场景。
30 2
Python的Virtualenv与Venv环境管理器
|
17天前
|
Python
Python软件包及环境管理器conda实战篇
详细介绍了如何使用conda进行Python软件包管理及环境管理,包括查看、安装、卸载软件包,切换源,管理不同版本的Python环境,以及解决使用过程中可能遇到的错误。
54 2
Python软件包及环境管理器conda实战篇
|
3天前
|
并行计算 开发者 Python
高效利用Python中的生成器提高内存管理
在处理大量数据或执行复杂计算时,内存管理成为关键问题。Python中的生成器(Generators)提供了一种优雅的解决方案,通过惰性计算和节省内存的方式显著提高程序的效率。本文将探讨生成器的基本概念,实际应用场景,以及如何利用生成器优化内存使用和提高程序性能。
|
5天前
|
Python Windows
安装Python环境
安装Python环境
21 8
|
24天前
|
Ubuntu 开发者 Python
|
4天前
|
监控 Ubuntu API
Python脚本监控Ubuntu系统进程内存的实现方式
通过这种方法,我们可以很容易地监控Ubuntu系统中进程的内存使用情况,对于性能分析和资源管理具有很大的帮助。这只是 `psutil`库功能的冰山一角,`psutil`还能够提供更多关于系统和进程的详细信息,强烈推荐进一步探索这个强大的库。
16 1
|
20天前
|
存储 程序员 Python
Python类的定义_类和对象的关系_对象的内存模型
通过类的定义来创建对象,我们可以应用面向对象编程(OOP)的原则,例如封装、继承和多态,这些原则帮助程序员构建可复用的代码和模块化的系统。Python语言支持这样的OOP特性,使其成为强大而灵活的编程语言,适用于各种软件开发项目。
15 1
|
22天前
|
Python
Python变量的作用域_参数类型_传递过程内存分析
理解Python中的变量作用域、参数类型和参数传递过程,对于编写高效和健壮的代码至关重要。正确的应用这些概念,有助于避免程序中的错误和内存泄漏。通过实践和经验积累,可以更好地理解Python的内存模型,并编写出更优质的代码。
13 2
|
22天前
|
Java Python
Python 中的内存管理
【8月更文挑战第29天】
25 1