编写Python代码以从定义的文件索引位置删除字节时遇到问题

编写Python代码以从定义的文件索引位置删除字节时遇到问题,python,python-3.x,Python,Python 3.x,我正在尝试编写一个简单的python3文件,该文件将列出文件目录中所需的(可见)文件,然后编辑每个文件,以便删除索引位置5到15处的字节,并将修改后的文件保存到输出文件夹中 我使用以下文件将文件名收集到输出文件中: #!/usr/bin/python import os import sys path = '.' print('Filenames and filename length below.') my_file = open("output_filenames.txt&qu

我正在尝试编写一个简单的python3文件,该文件将列出文件目录中所需的(可见)文件,然后编辑每个文件,以便删除索引位置5到15处的字节,并将修改后的文件保存到输出文件夹中

我使用以下文件将文件名收集到输出文件中:

#!/usr/bin/python 
import os
import sys

path = '.'
print('Filenames and filename length below.')
my_file = open("output_filenames.txt", "w")
my_file.close()
for file in os.listdir(path):
    current_file = os.path.join(path, file)
    print(current_file)
    print(len(current_file) - 2)
    print("""running getFilenames2.py , listing files from the directory once, 
    with clean_string2.py and new_editor.py""")
    x = current_file.strip("./")
    my_file = open("output_filenames.txt", "a")
    my_file.writelines(x)
    my_file.writelines('\n')
    my_file.close()
    # this file prints the filenames in the current directory. 
print("This is the end of getFilenames2.py")
接下来,我想去掉列表中生成的文件名中额外的“/”部分,并删除隐藏的文件名和完成所有这些操作所需的三个python文件。 在下一个文件中,我将列表复制到一个新文件中,然后修改此文件以删除不需要的文件名。有很多,因为我也试图通过部署到Pythonanywhere.com来实现这一点,这些是文件夹名和隐藏文件,或者我不想在它们的设置中列出、复制或修改的文件。 在要删除的文件列表的一半左右,我已经注释掉了一段代码,这段代码说明了我正在使用的一种新方法来删除不需要的文件。第92至108行。我应该切换到这种代码格式吗? 此外,我还担心在运行过程中删除数据,这就是为什么每次修改文件时,我都会写入一个全新的文件

#!/usr/bin/python 
import os
import sys
os.system('python getFilenames2.py')
my_file = open("output_filenames.txt", "r")
file_content = my_file.read()
my_file.close()
x = file_content
my_file = open("output_cleaned_filenames.txt", "a")
my_file.write(x)
my_file.close()
# print(x)
print(type(x))
# remove certain filenames from the list: DS_Store, clean_string.py, output_filenames.txt ,getFilenames.py , 
# output_cleaned_filenames.txt ,output_pruned_filenames.txt, getFilenames.py
# for online use of pythonanywhere website configuration, remove the following filenames:
# virtualenvs, vimrc, local, gitconfig, pythonstartup.py, bashrc, ipython, profile ,output_folder
my_file = open("output_cleaned_filenames.txt", "w")
my_file.write(x)
my_file.close()
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("DS_Store"):
            new_f.write(line)
my_file.close()
# the last seven lines stop DS_Store from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("virtualenvs"):
            new_f.write(line)
my_file.close()
# the last seven lines stop virtualenvs from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("vimrc"):
            new_f.write(line)
my_file.close()
# the last seven lines stop vimrc from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("local"):
            new_f.write(line)
my_file.close()
# the last seven lines stop local from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("gitconfig"):
            new_f.write(line)
my_file.close() 
# the last seven lines stop gitconfig from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("pythonstartup.py"):
            new_f.write(line)
my_file.close() 
# the last seven lines stop pythonstartup.py from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("bashrc"):
            new_f.write(line)
my_file.close() 
# the last seven lines stop bashrc from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("ipython"):
            new_f.write(line)
my_file.close()
# the last seven lines stop ipython from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("profile"):
            new_f.write(line)
my_file.close()
# the last seven lines stop profile from being listed in output_pruned_filenames.txt
# with open("output_cleaned_filenames.txt", "r") as f:
#    lines = f.readlines() 
#with open("output_pruned_filenames.txt", "w") as new_f:
#    pruned_new_f = new_f.strip("output_folder")
#    pruned_new_f.write(new_f)
#   
# infile = "output_pruned_filenames.txt"
# outfile = "cleaned_file.txt"

# delete_list = ["word_1", "word_2", "word_n"]
# with open(infile) as fin, open(outfile, "w+") as fout:
#     for line in fin:
#        for word in delete_list:
#           line = line.replace(word, "")
#        fout.write(line)
# my_file.close()
# the last seven lines stop output_folder from being listed in output_pruned_filenames.txt         
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("clean_string.py"):
            new_f.write(line)
my_file.close()
# the last seven lines stop clean_string.py from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("output_filenames.txt"):
            new_f.write(line)
my_file.close()
# the last seven lines stop output_filenames from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines() 
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("getFilenames.py"):
            new_f.write(line)
my_file.close()
# the last seven lines stop getFilenames.py from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines()
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("output_cleaned_filenames.txt"):
            new_f.write(line)
my_file.close()
# the last seven lines stop output_cleaned_filenames.txt from being listed in output_pruned_filenames.txt
with open("output_cleaned_filenames.txt", "r") as f:
    lines = f.readlines()
with open("output_pruned_filenames.txt", "w") as new_f:
    for line in lines:
        if not line.startswith("output_pruned_filenames.txt"):
            new_f.write(line)
my_file.close()
# the last seven lines stop output_pruned_filenames.txt from being listed in output_pruned_filenames.txt
my_file = open("output_pruned_filenames.txt", "r")
file_content = my_file.read()
my_file.close()
print(file_content)
print("This is the end of clean_string.py")
print('output_pruned_filenames.txt is the final output file')
print('These are the files in the current directory.') 
最后一个文件是最麻烦的,当复制到输出文件夹时,它将从每个列出的文件中删除一些字节。从索引5到索引14删除字节。 我将前5个字节复制到第一个输入文件,然后将索引15中的字节复制到输入文件的末尾,并将它们保存为第二个输出文件。 在此之后,我将输出文件连接在一起并放入第三个输出文件中。27号线有个问题。我应该还是指“工作文件”吗

strip()
rstrip()
不用于删除子字符串。它们删除列表开头或结尾的所有字符,而不仅仅是确切的字符串。您可以使用
re.sub()
删除开头或结尾的子字符串,例如

import re

new_filename = re.sub(r'\.txt$', '', filename)
您正在使写入所需的字节比需要的困难得多。您不需要额外的文件,只需对读取的字符串进行切片即可

with open(x, "r") as working_file:
    file_contents = working_file.read()
# code to calculate dynamic_filename
with open(os.path.join("output_folder", dynamic_filename), "w") as my_file:
    my_file.write(file_contents[0:5] + file_contents[15:])

你为什么要做
os.path.join(path,file)
然后去掉它添加的
/
?如果不需要目录前缀,首先不要添加它。
writelines()
通常与包含字符串的列表或其他iterable一起使用。如果您只编写一个字符串,只需使用
write()
。我不知道,我几周前刚刚学习了Python,我一直在将代码片段添加到一起,直到某些东西起作用。在目录中列出文件应该是os.path(file)。下面的代码用于生成目录中的文件名列表。对于os.listdir(path)中的文件,import os import sys path='.'print('下面的文件名和文件名长度')my_file=open(“output_filenames.txt”,“w”)my_file.close():current_file=os.path.join(“,file)#os.path.join(path,file)是原始代码段。非常感谢您的回答,我相信这将引导我编写一个实用的Python脚本。我想你的意思是说我应该连接file_内容[0:4]和file_内容[15:],因为我正在“删除”从第五个字节之后开始的十个字节。对不起,如果不清楚的话。我是对的,还是我遗漏了其他细节?我错过了edge case??Slice不包含第二个索引,因此
0:5
表示字节0到4。你能用Pathlib而不是os.path.join写同样的东西吗?我相信你能。谢谢Barmar,我感谢你对这个新手的帮助。
with open(x, "r") as working_file:
    file_contents = working_file.read()
# code to calculate dynamic_filename
with open(os.path.join("output_folder", dynamic_filename), "w") as my_file:
    my_file.write(file_contents[0:5] + file_contents[15:])