【发布时间】:2017-11-15 19:59:36
【问题描述】:
我的目标:构建一个程序:
- 从用户的计算机打开一个文件夹(由用户提供)
- 遍历该文件夹,打开每个子目录中的每个文档(根据语言代码命名;“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'
我不确定如何遍历子目录中的每个文件并使用新的唯一字符串更新每个文件中的字符串(不是文件名)。我以为我有它,但这个错误完全让我失望。在此之前,我收到一条错误消息,上面写着“不是文件或目录:'ExploringEduTubingEN(1).txt'”,令我惊讶的是,第一个错误可能会请求文件或 em> 一个目录,一旦我修复它,它只需要一个目录;似乎它应该在一开始就要求一个目录。
事不宜迟,代码(放置在底部,因为包含上下文很长):
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
如果你读到这里,谢谢。我试图提供尽可能多的信息,尤其是关于我的思考过程的信息。我会继续尝试和阅读,但我希望有更多的关注。
【问题讨论】:
-
您的输入文件真的是 pdf 文件吗?还是简单的文本文件?
-
PDF 文件,很遗憾。它们是我们发送的外部文件;我们有一个根据标准模板生成链接的团队,所以我认为这是一种节省时间的方法,并且实际上不会让用户输入那些 PDF 链接。我试图消除的部分过程是我们进入每个文档并添加来自该团队的独特 PDF。
-
我明白了,这不像您尝试打开文件并替换文本那么简单(使用文本文件很容易)。您在这里有两个问题:1)遍历文件列表,这很容易,2)替换pdf文件中的文本内容(如果我理解正确的话)。我建议为第二个问题打开一个单独的问题。
-
呃,不,抱歉。我误解了你的问题。我正在 输入 到文本文件的字符串实际上是 PDF 的链接(“example2.pdf”替换“example.pdf”),但我正在编辑的文件只是一个文本文件。