我需要对一些pdf文件进行一些操作。作为第一步,我想将它们从一个目录复制到支持我需求的树中。我用下面的代码为什么我不能打开已使用此代码复制的PDF文件
for doc in docList:
# these steps just create the directory structure I need from the file name
fileName = doc.split('\\')[-1]
ID = fileName.split('_')[0]
basedate = fileName.split('.')[0].split('_')[-1].strip()
rdate = '\\R' + basedate + '-' +'C' + basedate
newID = str(cikDict[ID])
newpath = basePath + newID + rdate
# check existence of the new path
if not os.path.isdir(newpath):
os.makedirs(newpath)
# reads the file in and then writes it to the new directory
fstring = open(doc).read()
outref = open(newpath +'\\' + fileName, 'wb')
outref.write(fstring)
outref.close()
当我运行这个代码时,目录被创建并且每个目录中都有正确名称的文件。但是,当我点击打开一个文件时,Acrobat会收到一个错误消息,通知我该文件已损坏,无法修复。
我能够使用
shutil.copy(doc,newpath)
替换最后一个四行复制文件 - 但我一直无法弄清楚,为什么我无法读取该文件作为一个字符串,然后把它写在一个新的位置。
有一件事我做的是比较有什么从源到文件内容是什么读取后读它已被写入后:
>>> newstring = open(newpath + '\\' +fileName).read()
>>> newstring == fstring
True
所以它不会出现内容被改变?
尝试'一个fstring =开放(DOC,“RB”)。阅读()' – dawg
相同的消息,但这次我驳回消息后,该文件没有打开。这至少是你应该发布的部分答案,如果你有时间对rb如何产生影响发表评论感谢 – PyNEwbie