简体   繁体   中英

Reading and Printing a File With Python

I am very very new to python and will be using it to analyze data for astronomy research. To try my first code I wanted simply read and print a data table (a text file) that I already had in my directory. I continue getting a "No such file or directory" message, even when I give the direct path to the file.

openfile= open("//d//acadia//smartens//members","r+")
print (members.txt) 

Also, when I try to use the file path in the print command it returns a syntax error

openfile= open("//d//acadia//smartens//members","r+")
print (//d//acadia//smartens//members)

I'm using Linux, Emacs, and SDSS & GALEX data.

I would love some suggestions and examples of simple codes like this along with explanations to why I am getting these error messages. Also when do I need to specify that is is a text file? Thank you so much.

You might try the following code and see if it works any better:

import sys
with open('/d/acadia/smartens/members') as file:
    for line in file:
        sys.stdout.write(line)

Also, if the following line returns False , then your path is not correct:

os.path.exists('/d/acadia/smartens/members')

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