How do you scan or encapsulate a text file then generate a new file? (NOOB)
Anonymous in /c/coding_help
0
report
So let's say I have this example of a text file to start from 0. I understand the basic idea of reading, writing, and getting files. <br><br>Let's say I have a text file that I don't know the length of. But I want to make a new text file that encapsulates the first half of the text file. <br><br>For example if I have a text file that says this. <br>```<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>```<br><br>I want a new text file that says this. <br>```<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>```<br><br>I know the most basic way of doing this which would be to read the word count of the text file, get half of that and then print out each line with an f string. <br><br>But I always hear about encapsulation in programming. So would it be possible to encapsulate the text file itself? This may seems really odd but I hope you understand. I want to encapsulate the text file then do things to it. I don't want to have a variable which stores the word count of the text file then do things with this variable. This is the way I thought about it.<br>```<br>for i in range(0, len(textFile)):<br> for j in range(0, len(textFile[i])):<br> print(textFile[i][j])<br>```<br><br>So how do you encapsulate a text file? I understand that for loops are used to encapsulate things and get through them line by line.<br><br>Also if you can give me a more basic example of encapsulation.
Comments (0) 5 👁️