0

I'm having trouble figuring out how to move all .log and .txt files in a certain folder and it's subdirectories to a new folder. I understand how to move one file with shutil. But, I tried to use a loop, unsuccessfully, to move all. Can someone help me with this? Thanks ....

 import os, os.path
 import re




 def print_tgzLogs (arg, dir, files):
   for file in files:
      path = os.path.join (dir, file)
      path = os.path.normcase (path)
      defaultFolder = "Log_Text_Files"
      if not defaultFolder.endswith(':') and not os.path.exists('c:\\Extracted\Log_Text_Files'):
         os.mkdir('C:\\Extracted\\Log_Text_Files')
      if re.search(r".*\.txt$", path) or re.search(r".*\.log$", path):

         os.rename(path, 'C:\\Extracted\\Log_Text_Files')
         print path


 os.path.walk('C:\\Extracted\\storage', print_tgzLogs, 0)

Below is the trace back error:

    Traceback (most recent call last):
  File "C:\SQA_log\scan.py", line 20, in <module>
    os.path.walk('C:\\Extracted\\storage', print_tgzLogs, 0)
  File "C:\Python27\lib\ntpath.py", line 263, in walk
    walk(name, func, arg)
  File "C:\Python27\lib\ntpath.py", line 263, in walk
    walk(name, func, arg)
  File "C:\Python27\lib\ntpath.py", line 263, in walk
    walk(name, func, arg)
  File "C:\Python27\lib\ntpath.py", line 259, in walk
    func(arg, top, names)
  File "C:\SQA_log\scan.py", line 16, in print_tgzLogs
    os.rename(path, 'C:\\Extracted\\Log_Text_Files')
WindowsError: [Error 183] Cannot create a file when that file already exists
Pit
  • 3,606
  • 1
  • 25
  • 34
suffa
  • 3,606
  • 8
  • 46
  • 66
  • 3
    You could easily use path.endswith(".txt") instead of re.search(r".*\.txt$", path). – utdemir Jun 23 '11 at 19:52
  • possible duplicate of [copy multiple files in python](http://stackoverflow.com/questions/3397752/copy-multiple-files-in-python) – Zsolt Botykai Jun 23 '11 at 19:55
  • @Zsolt Botkai - the problem is moving all .log and .txt files at the same time. – suffa Jun 23 '11 at 20:29
  • 1
    Shouldn't `shutil.move('time.log', 'C:\\...')` be `shutil.move(path, 'C:\\...')`? – Pit Jun 23 '11 at 20:43
  • I'm also a bit confused as to why you're using `shutil.move` on a hardcoded value (`time.log`) instead of on the variable `path`... – photoionized Jun 23 '11 at 20:45
  • @Pit - @photoionized I started with path but got an error. So, I tried just one file. – suffa Jun 23 '11 at 20:48
  • I just posted an answer, maybe `os.rename` helps you. If not, add the error with the traceback to your question so we can assist you further. – Pit Jun 23 '11 at 20:54
  • @Pit - I just added the traceback after trying os.rename .... – suffa Jun 23 '11 at 21:07
  • @user And that was really helpful in finding the error, as the traceback says: `Cannot create a file when that file already exists`. See my modified answer on how to solve it! – Pit Jun 23 '11 at 22:04
  • @Pit - I attached the recent traceback error. I'm double checking everything ... seems to be right. – suffa Jun 23 '11 at 22:17
  • @user Thanks for the traceback, just fixed my code once again. Hopefully it works this time! – Pit Jun 23 '11 at 22:26
  • @Pit - thanks ... worked like a charm. I have a better understanding of Python thanks to your help. – suffa Jun 23 '11 at 23:50
  • @user I'm happy the script worked for you, and even more happy to get you a better understanding of Python! Be sure that, whenever you encounter any errors, you read and try to understand them. And if you are unsure about how a method works, check the [Python docs](http://docs.python.org/), they are brilliant! – Pit Jun 24 '11 at 09:14

2 Answers2

1

According to the traceback, the log-files are already existing. The Python docs to the os.rename say:

On Windows, if dst already exists, OSError will be raised [...].

Now you can either:

  • delete the files manually or
  • delete the files automatically using os.remove(path)

If you want the files to be automatically deleted, the code would look like this (notice that I replaced your regular expression with the python endswith as suggested by utdemir):

import os, os.path

def print_tgzLogs (arg, dir, files):
    for file in files:
        path = os.path.join (dir, file)
        path = os.path.normcase (path)
        defaultFolder = "Log_Text_Files"
        if not defaultFolder.endswith(':') and not os.path.exists('c:\\Extracted\Log_Text_Files'):
            os.mkdir('C:\\Extracted\\Log_Text_Files')
        if path.endswith(".txt") or path.endswith(".log"):
            if os.path.exists('C:\\Extracted\\Log_Text_Files\\%s' % file):
                os.remove('C:\\Extracted\\Log_Text_Files\\%s' % file)
            os.rename(path, 'C:\\Extracted\\Log_Text_Files\\%s' % file)
            print path

os.path.walk('C:\\Extracted\\storage', print_tgzLogs, 0)
Pit
  • 3,606
  • 1
  • 25
  • 34
  • 1
    `os.rename` doesn't accept a directory name. Either use shutil.move or build a destination path that includes the file name. – Ray Jun 23 '11 at 22:24
1

It looks like are trying to use

os.rename(path, 'C:\\Extracted\\Log_Text_Files')

to move the file path into the directory C:\Extracted\Log_Text_Files, but rename doesn't work like this: it's going to try to make a new file named C:\Extracted\Log_Text_Files. You probably want something more like this:

os.rename(path, os.path.join('C:\\Extracted\\Log_Text_Files',os.path.basename(path))
sholte
  • 1,295
  • 1
  • 9
  • 11