Этот ответ был решен, делая print (file.read())
Проблемы с чтением и записью текстового файла в Python
У меня есть проект под названием «PyDOS». Недавно я обнаружил, что вы можете читать и записывать файлы в Python, я реализовал это, и бит записи работал. Но при попытке прочитать часть прочитает синтаксис. Код, который портя чтения части является:
print file.read
Это код с первой ошибкой:
def textviewer():
print ("Text Viewer.")
file_name = input("Enter a text file to view: ")
file = open(file_name, "r")
print file.read #This returns 'Syntax Error' when pressing F5
input("Press enter to close")
def edit(): #However, the writing function works just fine.
os.system('cls' if os.name == 'nt' else 'clear')
print ("EDIT")
print ("-------------")
print ("Note: Naming this current document the same as a different document will replace the other document with this one.")
filename = input("Plese enter a file name.")
file = open(filename, "w")
print ("Now, Write 5 lines.")
line1 = input()
line2 = input()
line3 = input()
file.write(line1)
file.write("\n")
file.write(line2)
file.write("\n")
file.write(line3)
file.close()
print ("File saved!")
time.sleep(3)
Это возвращает ошибку синтаксиса, я попытался file.read()
, но вместо этого показало:
<built-in method read of _io.TextIOWrapper object at 0x10ada08>
'print' - это не инструкция, а функция, а' file.read' - это метод, который необходимо * вызывать *. –