遍历子目录以向每个fi添加唯一的字符串

2024-04-20 02:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标:建立一个程序:

  • 从用户计算机打开文件夹(由用户提供)
  • 遍历该文件夹,打开每个子目录中的每个文档(根据语言代码命名;“AR”、“EN”、“ES”等)
  • 用中的一个字符串替换每个文档中的另一个字符串。最关键的是,根据文件夹名称中的语言代码,新字符串将随每个文档而改变(尽管旧字符串不会改变)。你知道吗

我的经验水平:最低限度;我已经学习python几个月了,但这是我构建的第一个不是数字绘制的程序。我建造它是为了让工作过程更快。我确信我并没有尽可能有效地构建它;我一直在用自己的知识和在构建它的过程中虔诚地阅读stackexchange来构建它。你知道吗

我自己做的研究:过去几天我一直住在stackexchange,但我没有发现有人做我正在做的事情(这让我非常惊讶)。我不确定这是否仅仅是因为我缺乏词汇表来搜索(尝试了很多搜索词,但没有一个完全符合我所做的),或者这仅仅是一种错误的处理方式。你知道吗

我遇到的问题: 我得到这个错误:

Traceback (most recent call last):
  File "test5.py", line 52, in <module>
    for f in os.listdir(src_dir):
OSError: [Errno 20] Not a directory: 'ExploringEduTubingEN(1).txt'

我不知道如何遍历子目录中的每个文件并用新的唯一字符串更新每个文件中的字符串(而不是文件名)。我以为我有,但这个错误完全把我甩了。在此之前,我在同一行中遇到了一个错误,上面写着“Not a file or directory:'ExploringEduTubingEN(1).txt'”,让我惊讶的是,第一个错误可能会请求一个文件一个目录,一旦我解决了这个问题,它只请求一个目录;似乎它应该在一开始就请求一个目录。你知道吗

无需进一步ado,代码(放在底部,因为包含上下文很长):

import os

ex=raw_input("Please provide an example PDF that we'll append a language code to. ")
#Asking for a PDF to which we'll iteratively append the language codes from below.

lst = ['_ar.pdf', '_cs.pdf', '_de.pdf', '_el.pdf', '_en_gb.pdf', '_es.pdf', '_es_419.pdf',
'_fr.pdf', '_id.pdf', '_it.pdf', '_ja.pdf', '_ko.pdf', '_nl.pdf', '_pl.pdf', '_pt_br.pdf', '_pt_pt.pdf', '_ro.pdf', '_ru.pdf',
'_sv.pdf', '_th.pdf', '_tr.pdf', '_vi.pdf', '_zh_tw.pdf', '_vn.pdf', '_zh_cn.pdf']
#list of language code PDF appending strings.

pdf_list=open('pdflist.txt','w+')
#creating a document to put this group of PDF filepaths in.

pdf2='pdflist.txt'
#making this an actual variable.

for word in lst:
  pdf_list.write(ex + word + "\n")
#creating a version of the PDF example for every item in the language list, and then appending the language codes.

pdf_list.seek(0)
langlist=pdf_list.readlines()
#creating a list of the PDF paths so that I can use it below.

for i in langlist:
    i=i.rstrip("\n")
#removing the line breaks.

pdf_list.close()
#closing the file after removing the line breaks.

file1=raw_input("Please provide the full filepath of the folder you'd like to convert. ")
#the folder provided by the user to iterate through.
folder1=os.listdir(file1)
#creating a list of the files within the folder
pdfpath1="example.pdf"
langfile="example2.pdf"
#setting variables for below

#my thought here is that i'd need to make the variable the initial folder, then make it a list, then iterate through the list.


for ogfile in folder1:
    #want to iterate through all the files in the directory, including in subdirectories
    src_dir=ogfile.split("/",6)
    src_dir="/".join(src_dir[:6])
    #goal here is to cut off the language code folder name and then join it again, w/o language code.
    for f in os.listdir(src_dir):
        f = os.path.join(src_dir, f)
        #i admit this got a little convoluted–i'm trying to make sure the files put the right code in, I.E. that the document from the folder ending in "AR" gets the PDF that will now end in "AR"
        #the perils of pulling from lots of different questions in stackexchange
        with open(ogfile, 'r+') as f:
            content = f.read()
            f.seek(0)
            f.truncate()
            for langfile in langlist:
                f.write(content.replace(pdfpath1, langfile))
                #replacing the placeholder PDF link with the created PDF links from the beginning of the code

如果你读到这里,谢谢。我试着提供尽可能多的信息,特别是关于我的思维过程。我会继续尝试和阅读,但我希望有更多的眼睛。你知道吗


Tags: oftheto字符串insrcforpdf
1条回答
网友
1楼 · 发布于 2024-04-20 02:19:49

您必须指定目录/文件的完整路径。使用os.path.join创建文件或目录的有效路径(与平台无关)。你知道吗

要替换字符串,只需使用子文件夹名称修改示例字符串。假设ex作为filename.pdf格式,您可以使用:newstring = ex[:-4] + '_' + str.lower(subfolder) + '.pdf'。这样,您就不必指定替换字符串的列表,也不必遍历该列表。你知道吗

解决方案

要遍历目录并根据需要替换文件内容,可以执行以下操作:

# Get the name of the file: "example.pdf" (note the .pdf is assumed here)
ex=raw_input("Please provide an example PDF that we'll append a language code to. ")

# Get the folder to go through
folderpath=raw_input("Please provide the full filepath of the folder you'd like to convert. ")

# Get all subfolders and go through them (named: 'AR', 'DE', etc.)
subfolders=os.listdir(folderpath)

for subfolder in subfolders:
    # Get the full path to the subfolder
    fullsubfolder = os.path.join(folderpath,subfolder)

    # If it is a directory, go through it
    if os.path.isdir(fullsubfolder):
        # Find all files in subdirectory and go through each of them
        files = os.listdir(fullsubfolder)
        for filename in files:
            # Get full path to the file
            fullfile = os.path.join(fullsubfolder, filename)

            # If it is a file, process it (note: we do not check if it is a text file here)
            if os.path.isfile(fullfile):
                with open(fullfile, 'r+') as f: 
                    content = f.read()
                    f.seek(0)
                    f.truncate()

                    # Create the replacing string based on the subdirectory name. Ex: 'example_ar.pdf'
                    newstring = ex[:-4] + '_' + str.lower(subfolder) + '.pdf'
                    f.write(content.replace(ex, newstring))

注意

您可以通过一个对话框让用户打开目录,而不是要求用户查找或写入文件夹。有关详细信息,请参见此问题:Use GUI to open directory in Python 3

相关问题 更多 >