Python OS experiences

Source: Internet
Author: User

A few days ago, I used a file replacement function.Python OSThe Python OS module is very powerful,
If you want your pythonProgramThis module is especially important if it has nothing to do with the platform. That is to say, it allows a program to run in Linux and Windows without any modification or any problems. It can be seen that python is a powerful cross-platform tool, the highlights are coming soon:

I will share with you the methods of the frequently used Python OS module below. If you use some good Python OS methods, you can send them to the python free man.Community

1. the OS. getcwd () function gets the current working directory, that is, the directory path of the current Python script. This is used for python development in Linux.
2. OS. listdir () returns the names of all files and directories in the specified directory.
3. OS. System () can be used to run shell commands. It is similar to Python popen, but it is somewhat different.

The three methods mentioned above are as follows:
You can create a new file in your current directory named test. py.

Test. pyCode:
# Encoding = UTF-8
Import OS
Path = OS. getcwd () # obtain the current path
Print OS. listdir (PATH) # display the file directories and files in the current path
Print OS. System ('Ping www.baidu.com ') # Ping speed to Baidu

The following are several other methods about files and directories in Python OS:
1. OS. path. split (name): Split the file name and directory (in fact, if you fully use the directory, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists

2. the OS. Path. isfile () and OS. Path. isdir () functions verify whether the given path is a file or a directory.
3. OS. Path. abspath (name): Obtain the absolute path.
4. OS. Path. splitext (): separates file names and extensions
5. OS. Path. Join (path, name): Connection directory and file name or directory
6. OS. Path. basename (PATH): returns the file name.
7. OS. Path. dirname (PATH): returns the file path.

I am creating a file. py file to implement the above method:
File. py code:
# Encoding = UTF-8
Import OS
Path = OS. getcwd () # obtain the current path. The path content is/home/Haha/python.
Print OS. Path. Split (PATH) # split the file name and directory. My result is ('/home/hahaha', 'python ')
Dir = OS. Path. Split (PATH) [0]
If OS. path. isdir (DIR): # If a directory is provided, this method returns true; otherwise, false is returned. If the isfile () method is used to determine whether a file is returned, true is returned. If the file is not provided, false is returned.
Print Dir, 'is a dir'
There are several remaining methods available for you to experiment on your own, which is better and helpful for you to familiarize yourself with the python OS module!

Old Wang Python provides Python books, hoping to help you.

OriginalArticlePlease indicate Reprinted from Old Wang python, this article address: http://www.cnpythoner.com/post/92.html

Author: Lao Wang @ Python tutorial
Old Wang Python provides Python-related Django tutorials and Python downloads!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.