-1

I am trying to move a file using the shutil module in Python. I keep getting this error:

Traceback (most recent call last):
  File "<input>", line 31, in <module>
  File "C:\Python27\lib\shutil.py", line 302, in move
    copy2(src, real_dst)
  File "C:\Python27\lib\shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
 IOError: [Errno 2] No such file or directory: 'MLR_Resi_Customer.txt'

I do not understand why I am getting no such file or directory. If instead of using a shutil.move(filename, new_dest) it will print the file name I am looking for.

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"

So I am not sure what I am doing wrong. The path to the files is correct and It seems to be able to print the file name just fine. Any help with those issues above is greatly appreciated.

martineau
  • 119,623
  • 25
  • 170
  • 301
Kyle K
  • 503
  • 1
  • 4
  • 9
  • It's probably looking for 'MLR_Resi_Customer.txt' in the current working directory - you need to create a path with the base joined to the filename for source (and maybe specify the destination better, either with a trailing / or the dest filename). – a p Mar 16 '17 at 18:59
  • 1
    Can you trim this down to a few lines demonstrating the bad file copy? That's a lot of code to look through. – tdelaney Mar 16 '17 at 19:04
  • `os.listdir()` returns filenames only (they don't include the directory path). That's why you're getting the file not found error. Either use `os.path.join()` to add the directory path to each filename in the list returned, or use `glob.glob()` instead which will return filenames with the path used in the call (assuming you provided one) attached to it. – martineau Mar 16 '17 at 19:12
  • SO I have tried the glob.glob() and I get the error "AttributeError: 'list' object has no attribute 'rstrip" – Kyle K Mar 16 '17 at 20:13

1 Answers1

0

Use shutil.move(glob.glob(f)[0],dest_whatever) and that should solve your problem by giving it an actual path to the file, although, if the file doesn't exist glob.glob will return an empty array.

Andria
  • 4,712
  • 2
  • 22
  • 38