1

I just started learning Python this month with no prior coding knowledge. Before I could even get to practice coding, I was stuck with opening & importing files! To this end, I have no idea how to fix the problems as a newbie, so I will try to explain as best as I can.

1. Opening files.

Working directory is correct. The csv file was created with Geany on the Ubuntu operating system.

name=[]
age=[]
height=[]
with open(’/home/stephanie/chapter5exercise.csv’, encoding=’utf-8’,mode=’r’,newline=’’) as
csv file:
    reader = csv.reader(csvfile, delimiter=’,’)
    for row in reader:
        name.append(row[0])
        age.append(row[1])
        height.append(row[2])
print("Done!")


**Error message:**

 File "<ipython-input-3-f801fef0d65e>", line 5
    with open(’/home/stephanie/chapter5exercise.csv’, encoding=’utf-8’,mode=’r’,newline=’’) as
              ^
SyntaxError: invalid character in identifier

2. Importing files

I downloaded a whatsapp chat text file for a coding exercise. It is stored in my Desktop environment. However, when I attempted to import it on Jupyter Notbook it didn't work.

I believe this is the problem, although I do not know how to work-around this:

the txt file's location as shown in my terminal: /Users/Mac/Desktop

while the working directory as shown on Jupyter is: /home/stephanie/Desktop

with open ("_chat.txt") as f:
    data = f.readlines()

**error message:** 
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-5-07cb5c381c47> in <module>
----> 1 with open ("_chat.txt") as f:
      2     data = f.readlines()

FileNotFoundError: [Errno 2] No such file or directory: '_chat.txt'```


Any input or advice is greatly appreciated. Thanks very much in advance! 

1
  • 2
    The first it's because is a weird quote that Python doesn't recognize. Use standard "s instead. For the second, double check your path, and/or use an absolute path to it if you want to be sure. Commented Apr 15, 2020 at 20:36

1 Answer 1

1

For the first error, try changing the apostrophes in the open line. For example, change ’/home/stephanie/chapter5exercise.csv’ to '/home/stephanie/chapter5exercise.csv'. Apparently, the apostrophe (’) that you are using is not the correct one.

For the second error, try giving the entire path of the txt file.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Bharat. I was able to easily fix the fist error with your help. For the second error, however, it did not work despite my attempt to use relative and absolute path.
Does the second error has to do with what I saw on the terminal " /Users/Mac/Desktop " instead of " /home/stephanie/Desktop " ? I struggle to route the file through the right path because I couldn't simply do something like " Desktop/_chat.txt ". Do you have any suggestions for this? Thank you.
Ok, one easiest way to do it is to upload it to your Jupiter Notebook, click on File >> Open, and upload _chat.txt there, so you can give the local path of the file directly in your code block (E.g. file = '/home/jovyan/FORMULA.rtf' ). You can use a tab if you are unsure about your path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.