In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.