Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\My Documents\textprocessing\test files\keep 6 latest file.py", line 37, in <module>
    shutil.move(file_name, dst_file)
  File "C:\Python31\lib\shutil.py", line 278, in move
    copy2(src, real_dst)
  File "C:\Python31\lib\shutil.py", line 113, in copy2
    copyfile(src, dst)
  File "C:\Python31\lib\shutil.py", line 66, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: 'Copy of New Text Document.txt'

i recive this error when running my code
what does the code do?? - it works though a directory and makes a list of all text files and sorts them by the modifed date once its sorted it then takes the 6 latest files and puts them in a temp folder and deletes the rest and then places thoses 6 files back in the root folder

# sorts files by modifed date, collects the 6 most recent files, and delete all others.
#created by dan holding v0.1 (23 Aug 2010)
import os, glob, time, shutil
root = '.'
date_file_list = []
date_file_set = []
#\/ puts files in root folder in to list
for folder in glob.glob(root):
    print ("folder =", folder)
    for file in glob.glob(folder + '\*.txt*'):
        stats = os.stat(file)
        lastmod_seconds = stats[8]
        date_file_tuple = lastmod_seconds, file
        date_file_set = set(date_file_list)
        date_file_list = list(date_file_set)
        #print (date_file_list)
        date, count = '', 0
        #\/works tho files and finds the 6 latest files
    for lastmod_seconds, path in date_file_list:
        folder, file_name = os.path.split(path)
        lastmod_tuple = time.localtime(lastmod_seconds)
        file_date = time.strftime("%d/%m/%y", lastmod_tuple)
        if file_date != date:
            count += 1
        file_time = time.strftime("%H:%M:%S", lastmod_tuple)
        print ("%-40s %10s %10s" % (file_name, file_date, file_time))
        if count >6:
        dst_file = (".\\file_back_ups\\")
        shutil.move(file_name, dst_file)
#\/ removes all other  files
        for f in glob.glob ('.' +'\*.txt*'):
            os.unlink (f)
            #print ('test 2')    
            #\/ moves the 6 files back to the root folder
            for file in glob.glob(dst_file + '\*.txt*'):
                shutil.move (file, root)

any help would be great to why i am reciving this error and what to do about it?

6 Years
Discussion Span
Last Post by danholding

I think you should put the full path at line 37 in your code

shutil.move(path, dst_file)

Also I think you should sort date_file_list only after the loop for file in glob.glob... .

Edited by Gribouillis: n/a


thanks for that i have just inserted that instead of my sinipit of code and it worked like a treat :) thanks man

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.