Understanding and practice of common methods of python's os module, such as os.getcwd() os.listdir(path) os.path.join(path1, path2) etc.

        When I came into contact with python-related code, I found that I usually import os in the header file, and then use os to call some methods in the program, so as to realize the operation of files or paths. Here is a summary of some methods under the os module that we usually encounter, and through a small case to connect the methods under the frequently used os module, so that we can apply what we have learned.

        The main ones used are the following:

os.getcwd() #Get the directory where your current .py file is located, that is, the location where your current .py file is located

os.listdir(path) #path is a self-defined directory path. This statement will return a string list. The elements in the list are the names of the files under the directory path path or the names of the directories under the directory path path. Because there may be another directory nested under the path.

os.path.join(path1, path2) #Used to connect two paths, the path after connection is equivalent to path1/path2.

os.path.abspath(filenmae) #filename is the name of a file. This statement is used to obtain the absolute path of the file filename. The absolute path has a beginning and a tail. This path is unique.

os.path.isdir(path), os.path.isfile(path), os.path.exists(path) #path is a path, which can correspond to a specific file or a directory. The three statements The meanings are to judge whether the current path is a directory, whether the current path is a file, and whether the current path exists. 

For the above functions, we use the code to test the following, and the comments indicate that I have written them in the code:

import os                                           
# 先用os.getcwd 获得我当前文件所处的路径                          
print('os.getcwd()==', os.getcwd())                 
#  os.getcwd()== /home/liubs/ottertune/test   这是打印输出
                                                    
# 再用os.listdir 获得当前文件所在目录下的所有文件,也就是同级别的所有文件,以字符串列表的形
# 注意,我当前文件所在目录下不止一个文件,还有其他的文件。                      
path1 = '/home/liubs/ottertune/test'                
print('os.listdir(path=path1)==', os.listdir(path=pa
# os.listdir(path=path1)== ['test01_changeconfig.py'
# operate_system_os.py是我现在运行的文件。                    
                                                    
                                                    
# os.path.join用于将两个路径进行相加,并且在两个字符串之间加上‘/’           
path2 = os.path.join(path1, os.listdir(path1)[1])   
print('os.path.join==', path2)                      
# path1是我定义的一个路径,然后获得该路径下的一个文件名,将其进行连接              
# 输出结果如下:os.path.join== /home/liubs/ottertune/test/t
                                                    
                                                    
# os.path.abspath获取文件的绝对路径,包括文件名                    
path3 = os.path.abspath('operate_system_os.py')     
print('os.path.abspath==', path3)                   
# os.path.abspath== /home/liubs/ottertune/test/opera
                                                    
# os.path.exists,os.path.isdir,os.path.isfile,分别为路径是
print(os.path.exists('test04_knobsselect.py'))      
# 这里打印出来的为:True     因为test04_knobsselect.py这个文件确实存在。

The printed results are as follows:

 Realization of the small case:

Question: Print out the absolute path information of the files with the test keyword in the current directory

# 1、获得当前所在的文件目录路径                                                                                      
path_str = os.getcwd()                                                                                 
# 打印出当前文件目录路径下所有的文件名或目录名                                                                               
path_list = os.listdir(path_str)                                                                       
# 在当前目录下新建一个目录,名字为 'this_test00'                                                                       
new_file_path = 'this_test00'                                                                          
# 定义关键字段,即要查找出的是所有带有关键字段的文件的路径                                                                         
sub_str = 'test'                                                                                       
# 查看新建的目录名是否已经在当前目录下的列表中存在                                                                             
# 如果不存在,则在当前目录下新建一个目录                                                                                  
if new_file_path not in path_list:                                                                     
    os.mkdir(new_file_path)                                                                            
# 遍历当前目录下的所有文件名和目录名                                                                                    
for i in path_list:                                                                                    
    # 如果是一个目录名,则删除该目录                                                                                  
    if os.path.isdir(i):                                                                               
        os.rmdir(i)                                                                                    
    # 如果是一个文件名,则查看文件名中是否包含关键字段,这里用到find方法,如果不包含则返回值为-1,不等于-1则是包含关键字段                                   
    if i.find(sub_str) != -1:                                                                          
        # 将当前文件所在的目录和包含关键字的文件名进行连接并进行打印。                                                               
        path = os.path.join(path_str, i)                                                               
        print(path)                                                                                    

The result is as follows:

 

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/127850880