python 复制文件夹_python复制文件夹结构在另一个目录下

1586010002-jmsa.png

I have some questions related to copying a folder structure. In fact, I need to do a conversion of pdf files to text files. Hence I have such a folder structure for the place where I import the pdf:

D:/f/subfolder1/subfolder2/a.pdf

And I would like to create the exact folder structure under "D:/g/subfolder1/subfolder2/" but without the pdf file since I need to put at this place the converted text file. So after the conversion function it gives me

D:/g/subfolder1/subfolder2/a.txt

And also I would like to add if function to make sure that under "D:/g/" the same folder structure does not exist before creating.

Here is my current code. So how can I create the same folder structure without the file?

Thank you!

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)))

解决方案

For me the following works fine:

Iterate over existing folders

Build the structure for the new folders based on existing ones

Check, if the new folder structure does not exist

If so, create new folder without files

Code:

import os

inputpath = 'D:/f/'

outputpath = 'D:/g/'

for dirpath, dirnames, filenames in os.walk(inputpath):

structure = os.path.join(outputpath, dirpath[len(inputpath):])

if not os.path.isdir(structure):

os.mkdir(structure)

else:

print("Folder does already exits!")

Documentation:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值