Python 如何显示文件内容,我打错电话了吗?
但是,我正在尝试打印内容:显示在它的位置Python 如何显示文件内容,我打错电话了吗?,python,file,Python,File,但是,我正在尝试打印内容:显示在它的位置 谢谢您没有正确地写入和读取文件 以下是如何写入文件: if choice==1: filename=input('Please enter file name: ') file=open(filename,'w') print('Enter integers to be written to file and press enter when done.') count=1 fox=1 while count >=1:
谢谢您没有正确地写入和读取文件 以下是如何写入文件:
if choice==1:
filename=input('Please enter file name: ')
file=open(filename,'w')
print('Enter integers to be written to file and press enter when done.')
count=1
fox=1
while count >=1:
filedata=str(input('Enter integer '+str(fox)+' : '))
count+=1
fox+=1
file.write(filedata)
if filedata=='':
break
print(file)
逐行读取文件:
filename = input('Please enter file name: ')
with open(filename, 'w') as f:
f.write("I love programming!")
给定您的代码,您希望收集列表中的所有数字
,然后当用户完成后,将列表
写入文件
您可以通过while
循环执行此操作:
with open(filename) as f_obj:
for line in f_obj:
print(line.strip())
您没有正确地写入和读取文件 以下是如何写入文件:
if choice==1:
filename=input('Please enter file name: ')
file=open(filename,'w')
print('Enter integers to be written to file and press enter when done.')
count=1
fox=1
while count >=1:
filedata=str(input('Enter integer '+str(fox)+' : '))
count+=1
fox+=1
file.write(filedata)
if filedata=='':
break
print(file)
逐行读取文件:
filename = input('Please enter file name: ')
with open(filename, 'w') as f:
f.write("I love programming!")
给定您的代码,您希望收集列表中的所有数字
,然后当用户完成后,将列表
写入文件
您可以通过while
循环执行此操作:
with open(filename) as f_obj:
for line in f_obj:
print(line.strip())
您的程序有两个基本问题:
文件
是一个文件描述符。这是一个可以用来阅读内容的对象。您可以通过f.read()
或逐行对文件中的行执行此操作:打印(行)
作为文件:
(用于读取)和带有open(文件名,“w”)的作为文件:
(用于写入),因为它们还可以确保文件描述符已正确关闭您的程序有两个基本问题:
文件
是一个文件描述符。这是一个可以用来阅读内容的对象。您可以通过f.read()
或逐行对文件中的行执行此操作:打印(行)
作为文件:
(用于读取)和带有open(文件名,“w”)的作为文件:
(用于写入),因为它们还可以确保文件描述符已正确关闭请修复缩进,使其与实际代码匹配。无法通过打印文件句柄获取文件内容请修复缩进,使其与实际代码匹配。无法通过打印文件句柄获取文件内容