We have a bunch of old files (1990s) that we're preparing to import into a new system. Some of these files have a forward slash / in their filename and we need to rename these before importing (they're going to end up in Azure).
We've got some python code that walks the directory:
for root, dirs, files in os.walk(base_path):
for filename in files:
path = os.path.abspath(os.path.join(root, filename))
And then attempts to rename any files it doesn't like the look of using os.rename
. This works fine except on files with a forward slash in the filename - even os.path.exists
fails on these files using the path
calculated above.
In os.path.exists
we've tried replacing:
- the colon with a forward slash
- the colon with an escaped forward slash /
- the colon with an escaped colon :
And we always get a False
back. And a "No such file or directory" error when attempting an os.path.rename
. The filename has a forward slash in it but it's treated by the OS as a colon : - at least that's what os.walk
reports the filename as.
We also get an IOError: [Errno 2] No such file or directory
when doing
fileobj = open(path)
We can't rename the files manually in the Finder. You'll notice the Finder displays the filename with the forward slash (sorry no rep so links to images): finder rename error
Or from the command line - the filename is displayed here with the colon: terminal - no such file or directory error
Windows won't list the files in File Explorer or Powershell.
So I was wondering if anyone had any ideas as to how to go about renaming these files or somehow getting rid of the forward slashes please?
Thanks in advance!