Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python读取和打印文件_Python_File - Fatal编程技术网

使用Python读取和打印文件

使用Python读取和打印文件,python,file,Python,File,我对python非常陌生,将使用它为天文学研究分析数据。要尝试我的第一个代码,我只需要读取并打印一个数据表,一个我已经在目录中的文本文件。我继续收到一条没有这样的文件或目录消息,即使我给出了文件的直接路径 openfile= open("//d//acadia//smartens//members","r+") print (members.txt) 另外,当我尝试在print命令中使用文件路径时,它会返回一个语法错误 openfile= open("//d//acadia//smarten

我对python非常陌生,将使用它为天文学研究分析数据。要尝试我的第一个代码,我只需要读取并打印一个数据表,一个我已经在目录中的文本文件。我继续收到一条没有这样的文件或目录消息,即使我给出了文件的直接路径

openfile= open("//d//acadia//smartens//members","r+")
print (members.txt) 
另外,当我尝试在print命令中使用文件路径时,它会返回一个语法错误

openfile= open("//d//acadia//smartens//members","r+")
print (//d//acadia//smartens//members)
我正在使用Linux、Emacs和SDSS&GALEX数据

我喜欢一些简单代码的建议和示例,以及为什么我会收到这些错误消息的解释。我什么时候需要指定它是文本文件?
非常感谢。

您可以尝试以下代码,看看它是否工作得更好:

import sys
with open('/d/acadia/smartens/members') as file:
    for line in file:
        sys.stdout.write(line)
此外,如果以下行返回False,则您的路径不正确:

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

我强烈建议你从开始,特别是当你开始的时候。这很有效,谢谢!你能解释一下为什么这是正确的方法吗?为什么需要“导入系统”?“stdout”是什么意思?为什么使用“writeline”来代替打印命令?写入和打印之间的区别很小。write不添加换行符,这在这里很方便,不需要这样做;noctis的替代品显示出你有一些优势,但实际上这是一个品味的问题。请仔细查看文件路径是如何编写的,并通读以获取所有后续问题的答案。