0

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))
ambmil
  • 115
  • 3
  • 9
  • You need to post the error you're getting. The full trackback is preferred. –  Jul 16 '20 at 13:17
  • 1
    To answer your question, no. There is no limitation on file types. However, a file which is in use (by a user or system) cannot be moved. – S3DEV Jul 16 '20 at 13:19
  • 1
    Additionally, are you reading the file only to test if it’s an Excel file? If so, have a look at `os.path.splitext()`; then test the extension using string compare. This will be **much** more efficient! – S3DEV Jul 16 '20 at 13:21
  • @S3DEV, i really have to read the file in the subsequent codes... but I can try your suggestion. Thank you – ambmil Jul 16 '20 at 13:24

1 Answers1

1

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))
ambmil
  • 115
  • 3
  • 9