如何解决显示错误路径的OS错误

时间:2019-06-23 08:15:56

标签: python

我想要代码可以删除文件夹中存在的所有文件,通过他的路径从文本文件读取该文件夹。我尝试了以下代码,但显示错误:

  

OSError:[WinError 123]文件名,目录名称或卷标签语法不正确:'F:\ New folder \ n'

我的原始路径是:F:\ New文件夹

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:\\")

预期结果:它必须从给定的路径中删除文件,但显示的路径不正确。

0 个答案:

没有答案