0

I'm on a mac, and I have a collection of files where some file names end in \r carriage-return (don't ask why).

I can rename those files and drop the trailing \r, but I don't want to - the files are in a Font package and on principle I don't want to mess with it.

I've tried these things; all failed.

f = open("Icon\r")    file not found
f = open(r"Icon\r")   file not found
f = open(U"Icon\r")   file not found
f = open("Icon")      as expected, file not found

Is there any way to make Python accept \r within a filename?

Salvatore
  • 10,815
  • 4
  • 31
  • 69
Bill Torcaso
  • 135
  • 1
  • 8
  • 9
    Are you sure it's a `\r`? How does python see it? ([`os.walk`](https://docs.python.org/3/library/os.html#os.walk) to check.) – Ouroborus Jun 28 '22 at 18:25
  • You could try coding a C program.... – Basile Starynkevitch Jun 28 '22 at 18:40
  • 2
    @Ouroborus: `os.walk` is going to show a lot more than needed, a mere `print(os.listdir())` would show all the entries in the working directory (the place an unqualified `open` call will be looking). I don't think Python imposes any restrictions on filenames beyond those of the OS, so if the OS allows file names to contain carriage returns, Python can use/create such files. `print(os.getcwd())` will tell you if you're in the working directory you expect to be in. – ShadowRanger Jun 28 '22 at 18:41
  • 1
    If those don't work, then the file name contains something else than you claim. – tripleee Jun 28 '22 at 18:42
  • 1
    Is it possible that you're in the wrong directory when you call `open()`? When I test this on my system, I can open the file just fine. – Dietrich Epp Jun 28 '22 at 18:43
  • The OP also posted https://stackoverflow.com/questions/72805695/please-re-open-my-question-about-carriage-return-in-filenames-on-mac which basically confirms this as a duplicate of [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Jun 29 '22 at 17:47

2 Answers2

2

There is no problem for Python with a filename having a trailing '\r' character, the only prohibited character is the path separator '/'.

If open("Icon\r") gives a file not found error, then the filename is not really "Icon\r".

Use os.listdir() to see what the filenames really are.

wim
  • 338,267
  • 99
  • 616
  • 750
  • I am sure that this file name ends with carriage-return, ordinal value 13: >>> ld = os.listdir("./Pattern Pack 1") >>> pprint(ld) ['Icon\r', 'PP1_readme', 'Pattern Pack #1 3D', 'Pattern Pack #1 Filled', 'Pattern Pack #1 Inverse', 'Pattern Pack #1 Outline'] >>> "\r" in ld[0] True >>> ord(ld[0][-1]) 13 >>> – Bill Torcaso Jun 29 '22 at 17:29
0

If your file- and operating-system does support, Python should be fine too:

import os
from pathlib import Path 

print(os.name) 
# 'posix'

print(Path('test\r'))
# PosixPath('test\r')
print(Path('test\r').exists())
# False

# Now, let's create an example folder containing a carriage-return (CR) in name
os.mkdir('test\r')
print(Path('test\r').exists())
# True

Afterwards in the console the directory can be found:

ls test*
'test'$'\r' 

See also

hc_dev
  • 8,389
  • 1
  • 26
  • 38