In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-10-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the most commonly used method of os module in Python is, which is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Recently in the AI medical competition in Tianchi, so there is no time to write articles, if there are any friends who want to do it together, you can talk to me in private!
* print ("dividing line") *
Why is it the most commonly used? here is the method that I often use by summarizing the code of the great gods, and it is also the inevitable method that helps to improve efficiency in practical engineering. All the articles I've written are for machine learning, and I don't consider web development and other methods used in Python development projects.
# coding:utf-8
Import os
# summarize the most commonly used methods in os module
"
> import os
> print (len (dir (os)
one hundred and forty nine
# the os module is very powerful and has many functions. Let's summarize it today.
# what methods have I used? in fact, compared with all the methods, they are commonly used
# no more than 20, or 10?
"
# 1. Os.getcwd () can view the working directory of the current program.
"
> > os.getcwd ()
'C:\\ Python27'
# 2. Os.path.exists determines whether the path exists. The return value is
# Boolean type.
> new_path = "F:/test_path"
> > print (os.path.exists ("F:/test_path"))
False
# 3. If it doesn't exist, we can use os.makedirs () to build
# create a file directory.
> > if not os.path.exists (new_path):
Os.makedirs (new_path)
Another function to create a folder in # os is os.mkdirs ()
# the difference between the two is that os.makedirs () can be established recursively
# folder, that is, you can create multi-level directories, while os.mkdirs ()
# only one-level directory can be created.
# give me a chestnut
> new_path = "F:/test_path_1/test_path_1"
> > if not os.path.exists (new_path):
Os.mkdir (new_path)
# WindowsError, system error.
Traceback (most recent call last):
File "", line 2, in
Os.mkdir ("F:/test_path_1/test_path_1")
WindowsError: [Error 3]: 'FRV hand testful pathways 1 move testful pathways 1'
"
"
# 4. Os.path.join, the path to link two
> path2 = "F:/test_path2/"
> path3 = "second_path"
> path2_and_path3 = os.path.join (path2, path3)
> path2_and_path3
'FRU Rank testworthy path2max secondhands path.'
# take another chestnut, in deep learning, we often put the file path
The # path is linked to the file name, and the resulting file is placed in a new
# folder, for this simple question, you can
# do it this way.
Data_dir = "F:/data_dir/train/"
Files_name = "* .jpg"
Results_dir = "F:/data_dir/results/"
All_data_dirs = os.path.join (data_dir, files_name)
All_results_dirs = os.path.join (results_dir, files_name)
If not os.path.exists (all_results_dirs):
Os.makedirs (all_results_dirs)
"
"
# 5. Os.path.basename returns the last layer file name of the file path.
> path2 = "F:/test_path2/"
> path3 = "second_path"
> path2_and_path3 = os.path.join (path2, path3)
> path2_and_path3
'FRU Rank testworthy path2max secondhands path.'
> os.path.basename (path2_and_path3)
'second_path'
> > os.path.basename (os.path.join (path2_and_path3, "001.jpg"))
'001.jpg'
"
"
# 5. Os.listdir () lists all files and folders in the current directory
> path_3 = "F:/test_path/test_path_1"
> os.listdir (path_3)
['mini_df_10.csv',' mini_df_3.csv', 'mini_df_4.csv'
'mini_df_5.csv','mini_df_6.csv', 'mini_df_7.csv'
'mini_df_8.csv',' mini_df_9.csv']
# Let's create a new folder.
> path_3 = "F:/test_path/test_path_1"
> > print (os.listdir (path_3))
['mini_df_10.csv',' mini_df_3.csv', 'mini_df_4.csv'
'mini_df_5.csv', 'mini_df_6.csv',' mini_df_7.csv'
'mini_df_8.csv', 'mini_df_9.csv',' New folder']
"
The above is the most commonly used method of os module in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.