2

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!

Ben wood
  • 21
  • 1
  • 5
  • Possible duplicate of [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) – Jonatas CD Jun 21 '18 at 10:56
  • 1
    Thanks @JonatasCD - the problem we have isn't with the file paths and we aren't doing this processing in Windows. The problem is to do with filenames that contain a forward slash. – Ben wood Jun 21 '18 at 11:12
  • But I understood that the referred question could provide a similar solution for you. Maybe I was wrong. Did you check it, @ben-wood? – Jonatas CD Jun 21 '18 at 11:18
  • Is the processing done on Linux? If so, this may help: https://stackoverflow.com/questions/9847288/is-it-possible-to-use-in-a-filename – Dušan Maďar Jun 21 '18 at 11:21
  • 1
    Yeah thanks @JonatasCD - we're using os.path.join to create our file paths but reading that question more closely (thanks for the nudge) perhaps we need to try prefixing the filename with the raw string r prefix. I'll try it... – Ben wood Jun 21 '18 at 11:24
  • 1
    Thanks @dm295 I read that one too - it made me think there is perhaps no way to rename these files except on a different OS – Ben wood Jun 21 '18 at 11:27

1 Answers1

-1

I used one of the solutions proposed in "Is it possible to use “/” in a filename?" thread. Here my version of it in python:

filename.replace('/', '\N{FULLWIDTH SOLIDUS}')

That works only if your target filesystem supports unicode in file names.