Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction to several practical file operations in python

Shulou Source: shulou.com Published: 2022-06-03 11:54:23 09月25日 Update

This article mainly introduces "several practical file operations in python". In daily operation, I believe many people have doubts about several practical file operations in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "several practical file operations in python". Next, please follow the editor to study!

1. Determine whether the specified directory exists:

Os.path.exists (input_folder)

two。 Determine whether the specified directory is a folder

Os.path.isdir (input_folder)

3. Determine whether the specified directory is a file

Os.path.isfile (input_folder)

4. Determine whether the specified file is a picture (determine what kind of picture a given file is)

There is no one to answer the question? The editor has created a Python learning exchange QQ group: find like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Import imghdrimg_list= {'jpg','bmp','png','jpeg','rgb','gif','pbm','ppm','tiff','xbm'} if imghdr.what (input_filename) not in img_list:print (not image)

5. Determine whether the specified txt (file) is empty

Import osif os.path.getsize ('test.txt') is 0: print (' test.txt is emptiness')

6. Read txt file contents by line

F = open ('test.txt', "r") lines = f.readlines () for line in lines: print line line = line.strip ('\ n') # remove the newline symbol'\ n' print line

7. Traverse all files under the specified directory folder

For file in sorted (glob.glob (os.path.join (input_folder,'*. *')): print (file)

8. Chinese symbols in compatible paths in python programs

For file in sorted (glob.glob (os.path.join (input_folder,'*. *')): file = unicode (file,'utf-8')

9. Determine whether the folder exists. If it does not exist, create it. If it exists, delete it and then create it:

There is no one to answer the question? The editor has created a Python learning exchange QQ group: find like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! If not os.path.exists ('folder1'): os.makedirs (' folder1') else: shutil.rmtree ('folder1') os.makedirs (' folder1')

10. Create a txt file and write it, if it exists, empty it and then write:

F = open ('test.txt', "wt") f.writelines (' test' +'\ n') f.close ()

11. Determine whether there are Chinese characters in the path (string) path_str:

# coding:utf-8''' encountered a problem and no one answered it? The editor has created a Python learning exchange QQ group: find like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! For ch in path_str.decode ('utf-8'): if u'\ u4e00'

Tags: Files learning folders utility directories questions answers yes like-minded help each other help each other study and exchange study tutorials boys buddies tutorials e-books videos like-minded Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Microsoft Linux OPPO Reno