首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从txt文件中删除标题的Python代码

从txt文件中删除标题的Python代码
EN

Stack Overflow用户
提问于 2019-04-18 07:40:59
回答 2查看 1.2K关注 0票数 2

我有一个包含数千个.txt文件的文件夹。我正在使用windows批处理代码从该文件夹内的所有.txt文件中删除标题(第1行到第82行)。问题是,这段代码对于相对较小的文件工作得很好,但是现在我需要在大文件上使用它,而代码根本没有响应。

有人能帮我在python上编写这个windows批处理的代码吗?提前谢谢你。

代码语言:javascript
复制
@echo off
for %%f in (*.txt) do (
    more +82 "%%f" > "%TEMP%\%%f"
    move /y "%TEMP%\%%f" "%%f" > nul
)
echo Done.
EN

回答 2

Stack Overflow用户

发布于 2019-04-18 08:44:47

可能有些过火了,但这可能会起作用:

代码语言:javascript
复制
import tempfile
from io import StringIO
data = StringIO()

file_path = r'C:\Users\...\...'

# Set the numder of lines you'd like to exclude
header_end = 82


### Read your data into a StringIO container (untested for directory read!)
for i in os.listdir(file_path):
    if i.endswith('.txt'):
        with open(os.path.join(file_path, i), 'r') as f:
            data.write(f.read())

        ### Split linkes by \n (newline)
        tokens = data.getvalue().split('\n')

        ### Rejoin with a newline, but start at the header index value plus one.
        output_str = '\n'.join(tokens[header_end + 1:])

        ### Create a tempfile with '.txt' suffix; print(path) to find out file location (should be in temp folder)
        fd, path = tempfile.mkstemp(suffix='.txt')
        try:
            with os.fdopen(fd, 'w') as tmp:
                tmp.write(output_str)
        except IOError:
            print('Error writing temp file.')


        ### To rcleanup and remove the file
        if os.path.isfile(path):
            try:
                os.remove(path)
            finally:
                os.unlink(path)
票数 0
EN

Stack Overflow用户

发布于 2019-04-18 22:03:15

在跳过前82行之前,PowerShell脚本不写入temp,而是将原始文件移动到bak文件。

代码语言:javascript
复制
foreach ($File in (Get-ChildItem *.txt)){
  $BakFile = $File.FullName -replace 'txt$','bak.txt'
  Move-Item $File $BakFile -Force
  Get-Content $BakFile | Select-Object -Skip 82 | Set-Content $File
}

关于在批处理命令/文件中包装相同的主题

代码语言:javascript
复制
powershell -NoP -C "foreach ($File in (Get-ChildItem *.txt)){$BakFile = $File.FullName -replace 'txt$','bak.txt';Move-Item $File $BakFile -Force;Get-Content $BakFile | Select-Object -Skip 82 | Set-Content $File}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55737348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档