简体   繁体   中英

Python 3.5: FileNotFoundError: [Errno 2] No such file or directory

I learned Python but hadn't used it in a few months so I am perhaps rusty. I am just trying to start a project by reading from a csv text file. I understand that there are several questions on this site about this error, but I haven't found one that fixes the issue. This is probably really basic and I wanted to figure this out without posting a question, but after several hours I really don't get it. Sorry if I missed a question that has answered this, but I have checked a lot of questions first. Here is all of my code:

import csv
with open('C:\\Users\\Ben.Brittany-PC\\Documents\\stats.txt', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in reader:
        print(row)

and the error:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Ben.Brittany-PC\\Documents\\stats.txt'

I'd guess it's the backslashes. Have you tried adding an r in front on your path to make r'C:\\Users\\Ben.Brittany-PC\\Documents\\stats.txt', 'rb' ? This will make the string be read raw and backslashes will not need to be escaped.

The file C:\\Users\\Ben.Brittany-PC\\Documents\\stats.txt does not exist. Check the path (case sensitive).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM