简体   繁体   English

Shutil.move 和 os.rename 无法传输的文件类型

[英]Filetypes that Shutil.move and os.rename cannot transfer

I am new to python.我是 python 的新手。 I tried to search for answers but I cannot find a exact match to my question.我试图寻找答案,但找不到与我的问题完全匹配的答案。 I am trying to move all non-Excel files to another folder.我正在尝试将所有非 Excel 文件移动到另一个文件夹。 However, there is an error when trying to move a.pbix file.但是,尝试移动 .pbix 文件时出现错误。 I wonder if there are only limited number of filetypes supported by shutil.move() and os.rename() in moving files.我想知道在移动文件中,shutil.move() 和 os.rename() 是否只支持有限数量的文件类型。 And, are there any workarounds?而且,有什么解决方法吗? Thank you.谢谢你。

UPDATE: The error is PermissionError.更新:错误是 PermissionError。 Actually, when I checked now the target folder, the file is transferred but the original file is retained.实际上,当我现在检查目标文件夹时,文件被传输但原始文件被保留。

Here is my sample code:这是我的示例代码:

files = os.listdir(os.getcwd())

for f in files:
    try:
        data = pd.read_excel(f)  # importing the file
    except:
        shutil.move("{}".format(f), r".\\Non_Excel_Files\{}".format(f))

It is now working.它现在正在工作。 Thanks to the suggestion of S3DEV.感谢S3DEV的建议。

files = os.listdir(os.getcwd())
for f in files:
    if os.path.splitext(f)[1] != ".xlsx":
        shutil.move("{}".format(f), r".\\Non_Excel_Files\{}".format(f))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM