Moving specific files to subdirectories in a directory - python

I am pretty new to Python, but I tried to learn the basics to help in my research in geology.

In any case, I have several files that, as soon as I extracted them from zip files (incidentally, a painfully slow process), create several hundred subdirectories with 2-3 files in each. Now I want to extract all these files ending in 'dem.tif' and put them in a separate file (not copy, but move).

Perhaps I tried to go to this deep end, but the code I wrote works without errors, so it should not find the files (which exist!), Because it gives me an else expression. Here is the code I created

import os src = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Extracted' # input dst = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Analyses' # desired location def move(): for (dirpath, dirs, files) in os.walk(src): if files.endswith('dem.tif'): shutil.move(os.path.join(src,files),dst) print ('Moving ', + files, + ' to ', + dst) else: print 'No Such File Exists' 
+5
source share
4 answers

First, welcome to the community and python! You can change your username, especially if you are often here. :)

I suggest the following (stolen from Mr. Beazley ):

 # genfind.py # # A function that generates files that match a given filename pattern import os import shutil import fnmatch def gen_find(filepat,top): for path, dirlist, filelist in os.walk(top): for name in fnmatch.filter(filelist,filepat): yield os.path.join(path,name) # Example use if __name__ == '__main__': src = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Extracted' # input dst = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Analyses' # desired location filesToMove = gen_find("*dem.tif",src) for name in filesToMove: shutil.move(name, dst) 
+7
source

Update: it turned out below that he / she actually calls the move function, which was the first in my answer.

There are a few more things to consider:

  • You have the order of the elements returned in each tuple from os.walk , but I'm afraid to check the documentation for this function .
  • Assuming you fixed this, also remember that you need to os.join over files , and you need os.join to execute each of these << 23>, not src
  • The above would be, I hope, if you print out the values ​​returned by os.walk and comment out the rest of the code in this loop.
  • With code that does potentially destructive operations such as moving files, I should always try code that just prints the parameters to shutil.move , until you make sure that this is correct.
+3
source

I think you have mixed up the way you use os.walk() .

 for dirpath, dirs, files in os.walk(src): print dirpath print dirs print files for filename in files: if filename.endswith('dem.tif'): shutil.move(...) else: ... 
+2
source

Any specific reason you need to do this in Python? Will a simple command shell not be easier? If you are using a Unix-like system or have access to Cygwin on Windows:

 find src_dir -name "*dem.tif" -exec mv {} dst_dir 
+1
source

Source: https://habr.com/ru/post/1381522/


All Articles