0

This code I currently have does not work because it cannot find the file name after I use path + i because it will output \PCA lab\imagess1 and not \PCA lab\images\s1 if I try to put it in the path I get a EOL error and if I try

(path + '' + i)

I also get a EOL error.

import os
path = r'C:\Users\joeyh\Desktop\PCA lab\images'
filelist = os.listdir(path)
for i in filelist:
    with open(path + i) as f:
        print(f)
  • [`os.path.join(path, i)`](https://docs.python.org/3/library/os.path.html#os.path.join) – khelwood Sep 30 '20 at 20:43
  • I still get the same problem. It is only when I run it through the for loop the error occurs because there is not a slash separating the two files. Edit I will try that – JoeyHoward988 Sep 30 '20 at 20:44

2 Answers2

0

You should either use forward slashes / or escaped backslashes \\ in your path string. The backslash is an escape character. This allows us to write new-line characters as "\n" and similar. So, if you actually want a backslash, you'll need to escape it, by using `"\"'.

0

You can use os.path.join to join 2 paths.
First import os
Then: os.path.join(path, i) to take what you want.

LoukasPap
  • 1,244
  • 1
  • 8
  • 17