要使用Python命令管理Linux存储,您可以使用os
和shutil
库
import os
def get_disk_space():
total, used, free = os.statvfs('/')
total_space = total.st_blocks * total.st_frsize
used_space = (total.st_blocks - free.st_blocks) * total.st_frsize
free_space = free.st_blocks * total.st_frsize
return total_space, used_space, free_space
total, used, free = get_disk_space()
print(f"Total space: {total} bytes")
print(f"Used space: {used} bytes")
print(f"Free space: {free} bytes")
import os
def create_directory(directory_path):
if not os.path.exists(directory_path):
os.makedirs(directory_path)
print(f"Directory '{directory_path}' created.")
else:
print(f"Directory '{directory_path}' already exists.")
create_directory("/path/to/your/new/directory")
import shutil
def delete_directory(directory_path):
if os.path.exists(directory_path):
shutil.rmtree(directory_path)
print(f"Directory '{directory_path}' deleted.")
else:
print(f"Directory '{directory_path}' does not exist.")
delete_directory("/path/to/your/directory")
import shutil
def copy_file(src_path, dest_path):
if os.path.exists(src_path):
shutil.copy2(src_path, dest_path)
print(f"File '{src_path}' copied to '{dest_path}'.")
else:
print(f"File '{src_path}' does not exist.")
copy_file("/path/to/your/source/file", "/path/to/your/destination/file")
import shutil
def move_file(src_path, dest_path):
if os.path.exists(src_path):
shutil.move(src_path, dest_path)
print(f"File '{src_path}' moved to '{dest_path}'.")
else:
print(f"File '{src_path}' does not exist.")
move_file("/path/to/your/source/file", "/path/to/your/destination/file")
这些示例展示了如何使用Python执行基本的Linux存储管理任务。您可以根据需要扩展这些功能以满足您的需求。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。