如何解决操作系统错误显示错误路径

2024-06-28 19:53:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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


Tags: path代码import路径文件夹osusagecontent