python 移动文件失败_使用shuti移动文件时出错

我试图创建一个简单的函数来查找以某个字符串开头的文件,然后将它们移动到一个新目录,但是我一直从shutil“IOError:[Errno 2]没有这样的文件或目录:‘18-1.pdf’”中获取以下类型的错误,即使该文件存在。import os

import shutil

def mv_files(current_dir,start):

# start of file name

start = str(start)

# new directory ro move files in to

new_dir = current_dir + "/chap_"+ start

for _file in os.listdir(current_dir):

# if directory does not exist, create it

if not os.path.exists(new_dir):

os.mkdir(new_dir)

# find files beginning with start and move them to new dir

if _file.startswith(start):

shutil.move(_file, new_dir)

我使用shuil不正确吗?

正确代码:import os

import shutil

def mv_files(current_dir,start):

# start of file name

start = str(start)

# new directory ro move files in to

new_dir = current_dir + "/chap_" + start

for _file in os.listdir(current_dir):

# if directory does not exist, create it

if not os.path.exists(new_dir):

os.mkdir(new_dir)

# find files beginning with start and move them to new dir

if _file.startswith(start):

shutil.move(current_dir+"/"+_file, new_dir)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值