Python: 如何将 N 行从一个文件移动到另一个文件
Python: How to move N lines from one file to another
我正在尝试这样的事情,但在 Python 而不是 PowerShell 中:
https://askubuntu.com/questions/1105376/how-to-move-n-lines-from-one-file-to-another
有人可以帮助我吗?我需要将前 100 行从一个文本文件移动(剪切,不仅是复制)到另一个 empty/new 文本文件。
提前致谢
x = 100
with open("C:/path/to/file/you/want/to/copy/from",'r') as f1:
data = f1.readlines()
with open("C:/path/to/file/you/want/to/copy/from",'w') as f1:
for line in data[x:]:
f1.write(line)
with open("C:/path/to/file/you/want/to/copy/to",'w') as f2:
for line in data[:x]:
f2.write(line)
我正在尝试这样的事情,但在 Python 而不是 PowerShell 中: https://askubuntu.com/questions/1105376/how-to-move-n-lines-from-one-file-to-another
有人可以帮助我吗?我需要将前 100 行从一个文本文件移动(剪切,不仅是复制)到另一个 empty/new 文本文件。
提前致谢
x = 100
with open("C:/path/to/file/you/want/to/copy/from",'r') as f1:
data = f1.readlines()
with open("C:/path/to/file/you/want/to/copy/from",'w') as f1:
for line in data[x:]:
f1.write(line)
with open("C:/path/to/file/you/want/to/copy/to",'w') as f2:
for line in data[:x]:
f2.write(line)