【发布时间】:2017-04-11 05:45:03
【问题描述】:
我有一些关于复制文件夹结构的问题。事实上,我需要将 pdf 文件转换为文本文件。因此,我在导入 pdf 的地方有这样一个文件夹结构:
D:/f/subfolder1/subfolder2/a.pdf
我想在“D:/g/subfolder1/subfolder2/”下创建确切的文件夹结构,但没有 pdf 文件,因为我需要将转换后的文本文件放在这个位置。所以在转换功能之后它给了我
D:/g/subfolder1/subfolder2/a.txt
我还想添加 if 函数以确保在“D:/g/”下创建之前不存在相同的文件夹结构。
这是我当前的代码。那么如何在没有文件的情况下创建相同的文件夹结构呢?
谢谢!
import converter as c
import os
inputpath = 'D:/f/'
outputpath = 'D:/g/'
for root, dirs, files in os.walk(yourpath, topdown=False):
for name in files:
with open("D:/g/"+ ,mode="w") as newfile:
newfile.write(c.convert_pdf_to_txt(os.path.join(root, name)))
【问题讨论】:
标签: python directory copy subdirectory