python 复制创建并文件_Python:复制文件时创建文件夹

我正在尝试创建一个shell脚本,将文件从一台计算机(员工的旧计算机)复制到另一台(员工的新计算机)。多亏了这里可爱的人,我已经可以复制文件了,但是我遇到了一个问题-如果我从一个有两个文件的目录出发:

C:\Users\specificuser\Documents\Test文件夹

…到这个目录。。。

C: \Users\specific用户\桌面

…我看到文件显示在桌面上,但这些文件所在的文件夹(测试文件夹)没有创建。在

以下是我使用的复制功能:#copy function

def dir_copy(srcpath, dstpath):

#if the destination path doesn't exist, create it

if not os.path.exists(dstpath):

os.makedir(dstpath)

#tag each file to the source path to create the file path

for file in os.listdir(srcpath):

srcfile = os.path.join(srcpath, file)

dstfile = os.path.join(dstpath, file)

#if the source file path is a directory, copy the directory

if os.path.isdir(srcfile):

dir_copy(srcfile, dstfile)

else: #if the source file path is just a file, copy the file

shutil.copyfile(srcfile, dstfile)

我知道我需要在目的地创建目录,只是不太确定怎么做。在

编辑:我发现我有一个类型(os.makedir操作系统而不是操作系统.mkdir). 我测试了它,它创建了它应该创建的目录。不过,我希望它创建一个新的目录,从它开始的地方。例如,在测试文件夹中有子测试文件夹。它已创建子测试文件夹,但不会创建测试文件夹,因为测试文件夹不是dstpath的一部分。有道理吗?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值