hello guys can anyone led me what i should do with cut from file
so how i can told my python open items.txt
then cut 20 lines
from beginning then paste it on Sold-items.txt
hello guys can anyone led me what i should do with cut from file
so how i can told my python open items.txt
then cut 20 lines
from beginning then paste it on Sold-items.txt
Small files solution.
fr = open('items.txt', 'w')
fw = open('Sold-items.txt', 'w')
f = fr.readlines()
fr.write('\n'.join(f[20:]))
fr.close()
fw.write('\n'.join(f[:20]))
fw.close()
Read up on readlines and writelines.
with open('Sold-items.txt', 'w') as f:
f.writelines(open('items.txt').readlines()[20:])
Please note that you may want to think about a slightly different solution for large files.
And after reading comments it appears that your question really wants this:
"Remove the first 20 lines from file A and put them into file B."
file_A = "items.txt"
file_B = "Sold-items.txt"
lines = open(file_A).readlines()
with open(file_A, 'w') as a, open(file_B, 'w') as b:
b.writelines(lines[:20]) # first 20 lines
a.writelines(lines[20:]) # the rest of the lines
Please edit the question for clarity as two out of three answers read it the same way and none of three answers understood the 'cut' part actually meant modify the original file until given more information.