0

I want to loop through hundreds of file names and rename them. The issue is that the file names are over 260 characters and contain characters that make os.rename not usable.

The code below runs with regular filenames but not with the files described earlier:

import os
filepath = "path"
path = os.chdir(filepath)
count = 1
for filename in os.listdir(path):
   new_file_name = f"{count}.csv"
   os.rename(filename,new_file_name)
   count += 1

When I run with the long file names, I get:

Traceback (most recent call last):
  File "C:\Users\sarah\Desktop\Python\program.py", line 7, in <module>
    os.rename(filename,new_file_name)
FileNotFoundError: [WinError 3] The system cannot find the path specified:

Is there a way of looping through these file names and renaming them?

  • Hi! https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python There are a couple of alternative ways to os.rename. Try shutil package or invoking your terminal using os.system. I hope either works! – LocoGris Sep 13 '20 at 23:14
  • Did you check if `os.listdir` returns the filenames correctly? – Wups Sep 13 '20 at 23:16
  • The code works fine for my test case. Are you sure the `path` folder exists? – Grayrigel Sep 13 '20 at 23:18
  • Adding a print(filename, new_file_name) statement mayshed some light on the issue. – C. Pappy Sep 14 '20 at 00:29
  • Locogris, thanks, I tried the pathlib module and got the same error. Wups, I tried the os.listdir and get the exact name of the file. Graygiel, yes, the path folder exists, the code will run on files with shorter names in the same folder. – Stacked_Sarah Sep 14 '20 at 00:37
  • Take a hard look at the particular filename that raises the exception and read this excellent article: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file – C. Pappy Sep 14 '20 at 01:06
  • A hint but not a solution: install `cmder` and use shell commands to rename. Cmder could handle this. – heLomaN Sep 14 '20 at 02:11

3 Answers3

1

Filenames cannot exceed 255 characters in Windows of Linux.

Linux:

touch 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'

touch: cannot touch ‘zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz’: File name too long

Windows:

c:\Users\cp>echo 'foo' > zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

The filename, directory name, or volume label syntax is incorrect.
C. Pappy
  • 739
  • 4
  • 13
1

[Windows specific] To overcome the issue with MAX_PATH limitation, replace

os.rename(filename,new_file_name)

with

os.rename("\\\\?\\" + filename, "\\\\?\\" + new_file_name)
user10101
  • 1,704
  • 2
  • 20
  • 49
0

why not try linux shell?

c=0
a="a"
t=".txt"
for file in `ls key*`; do
    newfile="$a$c$t"
    mv $file $newfile
    let c=c+1
done
herongwei
  • 1
  • 1