+ 2
Can someone help me with my code?
Write a Python program that will open a file named thisFile.txt and write every other line into the file thatFile.txt. For instance, This is line one This is line two This is line three.. all the way up to eleven then on the other file it only takes every other line so line This is line one This is line three This is line five ... up to line eleven my code f = open('thisFile.txt', 'r') w = open('thatFile.txt', 'w') count = 0 for line in f: if count % 2 == 0: w.write(line) count += 1 w.close() f.close()
10 Antworten
+ 11
[Edited: 04.11.2020: Added code to read and display the content of the final file: thatFile.txt]
This is a code that creates the basic file you have to read from.
#Create an input file:
txt = 'This is line'
with open('line_1_to_9.txt','w') as input_file:
for num in range(1,12):
input_file.write(f'{txt} {num}\n')
#use this input file to read from and write to output file
f = open('line_1_to_9.txt', 'r')
w = open('thatFile.txt', 'w')
count = 0
for line in f:
if count % 2 == 0:
w.write(line)
count += 1
w.close()
f.close()
#Then open the output file and check the content.
# read final output file;
f = open('thatFile.txt', 'r')
for line in f.readlines():
print(line.strip())
+ 10
... , please give a clear description what your problem is. You have done a code, you hopefully have testet it, now tell us what your problem is. What kind of errors do you have? Thanks!
+ 10
... , ok - i think i got it. You have an input file with a content like:
This is line one
...
This is line eleven
You open the input file, read the content and write each second line in a new file. Right?
+ 10
... , i am a bit confused. I did a try with your code and it worked fine - exactly as you defined it. So - what is the problem you have???
How did you create your input file? As is is a simple txt file, you can create it with a simple text editor. But is has to be in the same folder as the python file.
+ 3
This code shows the solution with a live transmission between the first und the second book:
https://code.sololearn.com/cPB5asMrV1rw/?ref=app
+ 1
You sholud not say write a program, you should ask ques or help
+ 1
Hi Lothar, the question is:
Write a Python program that will open a file named thisFile.txt and write every other line into the file thatFile.txt. You may use “this.txt” included with this assignment. ******"this.txt" wasn't included so I'm not sure if I make a dictionary or what.. I tried but it kept giving me an error so I removed it
Sample input and output files are:
for this file:
This is line one
This is line two
This is line three
This is line four
This is line five
This is line six
This is line seven
This is line eight
This is line nine
This is line ten
This is line eleven
that file:
This is line one
This is line three
This is line five
This is line seven
This is line nine
This is line eleven
+ 1
Lothar, thank you for helping me but how do I open the output file... its not working
+ 1
thank you JaScript
0
I did ask for help.. also that's my code. Sorry for any misunderstanding