用Python反斜杠抛出错误

2024-06-25 05:49:56 发布

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

其目的是删除所有*.properties文件,此代码也可用于其他用途,因此使用相对路径和联接。 我不能跑了

DestDirName ="..\\Folder\\name\\present\\here\\"
Destbase_filename='*'
filename_suffix = '.properties'
Destfn = os.path.join(DestDirName, Destbase_filename + filename_suffix)
os.remove(Destfn)

这是错误的

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect:

请建议


Tags: 文件代码name目的hereospropertiesfolder
1条回答
网友
1楼 · 发布于 2024-06-25 05:49:56

os.remove一次只能删除一个文件。您需要逐个删除这些文件。使用glob.glob。你知道吗

    import glob
    files = glob.glob(Destfn)
    for file in files:
         os.remove(file)

相关问题 更多 >