For example, if the input file is:
def main():
for i in range(10):
print("I love Python")
print("Good bye!")
Then the output would be:
1 def main():
2 for i in range(10):
3 print("I love Python")
4 print("Good bye!")
I have difficulty in adding lines to each line. My program is:
filename = input("Please enter a file name: ")
count = 0
openfile = open(filename, "r")
for lines in openfile:
linenumbers = openfile.write(str(count)+'\t'+lines)
count += 1
print(count)
解决方案
You should add:
newFile = open(yourfile, 'w')
count = 1
for line in readfile:
newFile.write (str(count) + '\t' + line)
count += 1
newFile.close()
If you just want to print to console write (this is according to the variable names you've used in your second edit):
for lines in openfile:
print str(count) + '\t' + lines
count += 1
You should do your homework yourself, though!