简体   繁体   中英

Filetypes that Shutil.move and os.rename cannot transfer

I am new to 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. However, there is an error when trying to move a.pbix file. I wonder if there are only limited number of filetypes supported by shutil.move() and os.rename() in moving files. And, are there any workarounds? Thank you.

UPDATE: The error is 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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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