开发者

How to move all .log and .txt files to a new folder

开发者 https://www.devze.com 2023-03-15 12:50 出处:网络
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

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开发者_C百科, 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


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)


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号