+ 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()

3rd Nov 2020, 6:14 PM
...
... - avatar
10 Réponses
+ 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())
3rd Nov 2020, 9:27 PM
Lothar
Lothar - avatar
+ 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!
3rd Nov 2020, 7:53 PM
Lothar
Lothar - avatar
+ 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?
3rd Nov 2020, 9:05 PM
Lothar
Lothar - avatar
+ 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.
3rd Nov 2020, 9:14 PM
Lothar
Lothar - avatar
+ 3
This code shows the solution with a live transmission between the first und the second book: https://code.sololearn.com/cPB5asMrV1rw/?ref=app
3rd Nov 2020, 10:05 PM
JaScript
JaScript - avatar
+ 1
You sholud not say write a program, you should ask ques or help
3rd Nov 2020, 6:22 PM
Bhavik Mahalle
Bhavik Mahalle - avatar
+ 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
3rd Nov 2020, 8:12 PM
...
... - avatar
+ 1
Lothar, thank you for helping me but how do I open the output file... its not working
3rd Nov 2020, 10:03 PM
...
... - avatar
+ 1
thank you JaScript
3rd Nov 2020, 10:08 PM
...
... - avatar
0
I did ask for help.. also that's my code. Sorry for any misunderstanding
3rd Nov 2020, 6:27 PM
...
... - avatar