簡體   English   中英

使用Python讀取和打印文件

[英]Reading and Printing a File With Python

我對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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM