Python 使用shutil.move()移动文件时出现问题

Python 使用shutil.move()移动文件时出现问题,python,shutil,file-moving,Python,Shutil,File Moving,我正在尝试使用Python中的shutil模块移动文件。我不断地发现这个错误: 回溯(最近一次呼叫最后一次): 文件“”,第31行,在 文件“C:\Python27\lib\shutil.py”,第302行,移动中 副本2(src、real_dst) 文件“C:\Python27\lib\shutil.py”,第130行,在copy2中 复制文件(src、dst) copyfile中第82行的文件“C:\Python27\lib\shutil.py” 开放式(src,'rb')作为fsrc: I

我正在尝试使用Python中的
shutil
模块移动文件。我不断地发现这个错误:

回溯(最近一次呼叫最后一次):
文件“”,第31行,在
文件“C:\Python27\lib\shutil.py”,第302行,移动中
副本2(src、real_dst)
文件“C:\Python27\lib\shutil.py”,第130行,在copy2中
复制文件(src、dst)
copyfile中第82行的文件“C:\Python27\lib\shutil.py”
开放式(src,'rb')作为fsrc:
IOError:[Errno 2]没有这样的文件或目录:“MLR\u Resi\u Customer.txt”
我不明白为什么我没有得到这样的文件或目录。如果不使用
shutil.move(filename,new\u dest)
它将打印我要查找的文件名

import shutil
import os
import datetime

# set location of folders and files needed
source = '//nspinfwcipp01/BSA/marketing'
dest_cust = '//nspinfwcipp01/BSA/marketing/res_cust'
dest_pros = '//nspinfwcipp01/BSA/marketing/res_pros'
dest_win = '//nspinfwcipp01/BSA/marketing/res_win'

# create date time stamp of now
dt = str(datetime.datetime.now())

files = os.listdir(source)
print files

# create new path to storage files for old data
cust_files = os.listdir(dest_cust)
pros_files = os.listdir(dest_pros)
win_files = os.listdir(dest_win)

# new file names
new_cust = 'MLR_Resi_Customers'+dt+'.txt'
new_pros = 'MLR_Resi_Prospects'+dt+'.txt'
new_win = 'MLR_Resi_Winbacks'+dt+'.txt'

   #move files from marketing folder into their own folder when after done     processing

for f in files:
    if (f.endswith("Customer.txt")):
        print f
        shutil.move(f,dest_cust)
    elif (f.endswith("Prospects")):
        #print f
         shutil.move(f,dest_pros)
    elif (f.endswith("Winbacks")):
        #print f
        shutil.move(f,dest_win)

##rename files in storage with data for Kalyan and Jake's Project

## rename customer file for storage
for x in cust_files:
    if (x.endswith("Customers")):
        #print x
        new_cust = 'MLR_Resi_Customer'+dt+'.txt'
        os.rename('MLR_Resi_Customers.txt', new_cust)
    else:
        print "no_customer_file"

## rename prospect file for storage
for x in cust_files:
    if (x.endswith("Prospects")):
        #print x
        os.rename('MLR_Resi_Prospects.txt', new_pros)
    else:
        print "no_prospect_file"

## rename winback file for storage
for x in cust_files:
    if (x.endswith("Winbacks")):
        #print x
        os.rename('MLR_Resi_Winbacks.txt', new_win)
    else:
        print "no_winback_file"

所以我不确定我做错了什么。文件的路径是正确的,并且似乎可以很好地打印文件名。非常感谢您对上述问题的任何帮助

使用
shutil.move(glob.glob(f)[0],dest_which)
这将通过为文件提供实际路径来解决您的问题,如果文件不存在
glob.glob
将返回一个空数组。

它可能在当前工作目录中查找“MLR\u Resi\u Customer.txt”-您需要创建一个路径,将基连接到源文件名(并可能更好地指定目标,使用尾随/或dest文件名).你能把这段删减到几行来说明文件副本有问题吗?要查看的代码太多了。
os.listdir()
只返回文件名(不包括目录路径)。这就是为什么你得到的文件找不到错误。使用
os.path.join()
将目录路径添加到返回列表中的每个文件名中,或者使用
glob.glob()
,它将返回附加在调用中使用的路径(假设您提供了路径)的文件名。因此,我尝试了glob.glob()并得到了错误“AttributeError:'list'对象没有属性'rstrip”