1

I recently got the marks for a program I wrote for an assignment and I almost failed. The assignment was to create an address book that a user could add to and amend, with some other features.

The issue was with the import of my .txt file as, when the marker tried to run it, it couldn't locate the file and therefore couldn't run. It ran with no issue for me, which is why I didn't spot it, and so I assume that there is an issue with finding the directory when run on another PC.

This is the code that failed me:

import csv
filepath = 'data.txt'
with open(filepath, newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

I can see some smaller issues with this, e.g. I left 'newline' from some previous code that was changed, but I don't know what I should have done differently. I also don't think that the issue is with importing the .txt file as a CSV as this worked fine when writing and saving to the file on my PC.

Any help with this would be really appreciated.

Edit

I forgot to add that my uni gave a framework for importing the file (which I didn't use) which was this:

import os
save = False
last = -1
data = []

Not sure if this is helpful but thought it would be best to include it

2
  • 1
    filepath = 'data.txt' is a relative data path. This means it is grabbed from the directory where you are running the python code, which may exist locally for you, but not someone else. Where is the txt file the other person is trying to run? Commented Jan 22, 2021 at 15:11
  • I though that might have been the issue. We submitted the file and the code as a zip file, so I'm not sure where he would have unpacked it to. Commented Jan 22, 2021 at 15:22

3 Answers 3

2

Your code assumes that data.txt file exists. And crashes if it doesn't. Either you need to make very sure that you package data.txt together with your code and provide instructions to unpack them into the same directory or you need to handle it somehow. Maybe create it if it is missing.

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

Comments

0

please give the correct path of file if you are trying for windows . and than to debug it further try to print the data value as below :

import csv
filepath = 'C:/Users/diwak/Desktop/config.txt'
with open(filepath, newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

print(data)

6 Comments

It sounds like they are submitting their code for grading and it is being run on some other computer. A full path like you included is not guaranteed to exist. A relative path is actually better in this case.
hi Mad , agreed , than the other guys should choose the relative path for this guy's code on the other computer accordingly and than run the code for him to check they are getting expected results .
That's exactly how it was submitted, Mad. Because of that, I'm not sure how to rectify it. Others on my course got good marks so they must have done it correctly.
hi shady , as per my understanding try giving a common path whic exists basically in all systems for example in windows first create a file on path c:/data.txt and than use it .On Linux create a file on /home/data.txt or /root folder/data.txt with the inputs you want to add and than run your code
@shady see my answer here. The most obvious solution is to catch the "file not found" exception and create the file. Also, the newline='' thing seems suspicious. Are you sure that is what you want to do?
|
0

this error is common, thoses kind of error occur depending how you run your script. I tied to reproduce your problem, I indeed succeeded. For exemple, I ran your script in the cmd and it gave me no errors. But when I ran it in my IDE with the"run function" I get the error "FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'". This is really hard to fix because its not really your fault. You should really talk to your teacher about that. She will show you the wright way to do it.

Comments

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.