-1

I want to iterate over a number of files in the L: drive on my computer in a folder called 11109. This is my script:

for filename in os.listdir('L:\11109'):
    print(filename.split('-')[1])

However the error message comes back as :

File "L:/OMIZ/rando.py", line 12, in <module>
for filename in os.listdir('L:\11109'):

FileNotFoundError: [WinError 3] The system cannot find the path specified: 
'L:I09'

Its reading the L:\ 11109 fine but the error message said the path specified is L:I09?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
matthewr
  • 192
  • 1
  • 1
  • 13
  • I suggest that you use raw strings for Windows paths. FWIW, `\111` is an octal escape sequence, and `'\111'` is the same string as `'I'`. 111 is octal for 73, and 73 is the codepoint of `'I'`, that is, `chr(73)` returns `'I'`. – PM 2Ring Jul 13 '18 at 11:13

2 Answers2

1

You need to use raw strings or escape the backslash, otherwise \111 is resolved to I:

a = 'L:\11109'
print(a)  # shows that indeed 'L:I09'

b = r'L:\11109'
print(b)  # prints 'L:\11109'

c = 'L:\\11109'  # will be understood correctly by open()
IonicSolutions
  • 2,559
  • 1
  • 18
  • 31
0

To solve this you can do like this

for filename in os.listdir('L:/11109'):
    print(filename.split('-')[1])
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108