Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to several practical file operations in python

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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'

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report