Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

Issue Importing File in Python

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

question from:https://stackoverflow.com/questions/65847678/issue-importing-file-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...