我想要的代码可以删除文件夹中存在的所有文件,从文本文件中按路径读取文件夹。我尝试了以下代码,但显示错误:
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'F:\New folder\n'
我的原始路径是:F:\New folder
import os
import shutil
import time
def get_drive_usage(path):
usage = shutil.disk_usage(path)
BytesPerGB = 1024 * 1024 * 1024
percent_utilization = ("Used_percent: %.2f" % (float(usage.used/usage.total)*100))
print(percent_utilization)
print("Total: %.2fGB" % (float(usage.total)/BytesPerGB)),
print("Used: %.2fGB" % (float(usage.used)/BytesPerGB)),
print("free: %.2fGB" % (float(usage.free)/BytesPerGB))
if percent_utilization > '20':
file = open('folder_name','r')
content = file.read()
print(content)
print(os.listdir(content))
for f in os.listdir(content):
os.remove(os.path.join(content,f))
print("file removed")
get_drive_usage("F:\\")
预期结果:它必须从给定路径中删除文件,但显示的路径不正确
目前没有回答
相关问题 更多 >
编程相关推荐