Opening a txt file in Python on a Mac

22,262

The \ escape isn't needed when the file path is in a string.

Change /Week\ 7/ to /Week 7/

Share:
22,262
Jessica
Author by

Jessica

Updated on December 18, 2020

Comments

  • Jessica
    Jessica over 3 years

    I am trying to open and read in the data from a txt file using Python. So far I have the following code:

    infile = open("/Users/jessicazahra/Desktop/University/Week\ 7/unsorted_fruits.txt", "r")
    outfile=open("/Users/jessicazahra/Desktop/University/Week\ 7/sorted_fruits.txt","w")
    

    I keep receiving an error stating:

    File "/Users/jessicazahra/Desktop/University/Week 7/sort_fruits.py", line 1, in <module>
        infile = open("/Users/jessicazahra/Desktop/University/Week\ 7/unsorted_fruits.txt", "r")
    FileNotFoundError: [Errno 2] No such file or directory: '/Users/jessicazahra/Desktop/University/Week\\ 7/unsorted_fruits.txt'
    

    I am using a mac and Python 3.4.1. I have a feeling something is wrong with my directory path but I cannot find what the problem might be though. The file is placed in a folder on my desktop. Any help would be much appreciated!!