Python-traversing directories

A language, if you know the basic grammar, know the input and output, file operations, database operations, communication (socket, etc.), you can basically start development. The remaining knowledge is slowly learned and accumulated in the development. This is also the first part of learning a new language. Nowadays, there are so many different languages, but in the final analysis, it is these contents.

You may also feel lost in learning, as if you don't know how to use new things. So, seeing all the points above, just start to do the programs that you have in mind.

Having said so much nonsense, enter the topic... I once wrote C language traversal directory, PHP traversal directory, today I will talk about python traversal directory, which is also a comparison for everyone. And learn what I said above: file operations.


The purpose of the program is to traverse a certain directory and replace a certain section of content in all files in it.

import os

def replace_str(FilePath,SourceStr,ObjectStr):
 file =open(FilePath,"r")
 str = file.read()
 str = str.replace(SourceStr,ObjectStr)
 file.close()
 file =open(FilePath,"w")
 file.write(str)
 file.close()

def getfile(path):for file in os.listdir(path):
  file = os.path.join(path,file)if os.path.isdir(file):getfile(file)else:replace_str(file,"abcd","")

rootpath ="e:\\01"getfile(rootpath)

We want to traverse the directory, first import the os module. import os

Skip the two functions first and see our bottom two lines. We just need to traverse the rootpath directory and pass it to the getfile function.

getfile is a recursive function, recursion must be used to traverse directories. Use os.listdir() to get all files (including directories and files), get a list, use for ..in to traverse the list, and then use the os.path.isdir() function to determine whether the current file is a directory .

If it is a directory, the getfile() function is executed recursively, otherwise, it enters the replace_str() function.

This is probably the process. The code is also very simple, combined with my previous two articles, it should not be difficult to understand. The main thing is to compare and see what are the advantages and disadvantages of each of the three languages to complete a task.


Supplement:

Traverse the directory, in fact, the os module of python has already given a function.

    generator = os.walk(top,topdown=True,onerror=None) 

The object it returns is a generator type object, we have to use the for statement to traverse it:

generator = os.walk(r"E:\Python\pro")for i in generator:
 print i

We will find that i is actually a ternary tuple, which is the directory at this time, the folders under this directory, and the files under this directory. So our getfile function above can be simplified as follows:

def getfile(path):
 generator = os.walk(path)for(now_dir, _, file_list)in generator:for file in file_list:
   file = os.path.join(now_dir, file)replace_str(file,"abc","")

Recommended Posts

Python-traversing directories