Python 3 – чтение текста из файла [дубликат]Python

Программы на Python
Ответить
Anonymous
 Python 3 – чтение текста из файла [дубликат]

Сообщение Anonymous »

Это упражнение 15 из книги «Изучите Python на собственном горьком опыте», но я использую Python 3.

Код: Выделить всё

from sys import argv
script, filename = argv

txt = open(filename)
print ("Here's your file %r:") % filename
print txt.read()

print ("I'll also ask you to type it again:")
file_again = input()
txt_again = open(file_again)
print txt_again.read()
файл сохраняется как ex15.py, и когда я запускаю его с терминала, он правильно читает ex15.txt в первый раз, но когда я запрашиваю его во второй раз, Я получаю сообщение об ошибке[/b]

Код: Выделить всё

user@user:~/Desktop/python$ python ex15.py ex15.txt

Here's your file 'ex15.txt':

This is stuff I typed into a file.

It is really cool stuff.

Lots and lots of fun to have in here.

I'll also ask you to type it again:

ex15.txt #now I type this in again, and I get a following error[/b]

Traceback (most recent call last):

File "ex15.py", line 11, in 

file_again = input()

File "", line 1, in 

NameError: name 'ex15' is not defined
Что случилось?

Подробнее здесь: https://stackoverflow.com/questions/129 ... rom-a-file
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»