A little bit of Python OS experience

Source: Internet
Author: User
Tags python script

A few days ago did a file replacement function to use somepython osThe functionality of the Python OS module is very powerful,
This module is especially important if you want your Python program to be platform-agnostic. That is, it allows a program to be written without any changes, and will not have any problems, you can run under Linux and Windows, see here think Python's powerful cross-platform, wonderful still behind:

The following is a list of the Python OS modules I often use to share with you, if you use some of the better Python OS methods, you can send to the Python Free People Community

1. The OS.GETCWD () function gets the current working directory, which is the directory path of the current Python script work, which is used on Linux to develop things with python.
2. Os.listdir () returns all file and directory names under the specified directory.
3. Os.system () can be used to run shell commands, similar to Python popen, but somewhat different

The 3 methods mentioned above, we do the following experiment:
You can create a new file in your current directory with the name test.py

test.py Code:
#encoding =utf-8
Import OS
Path = OS.GETCWD () #取得当前的路径
Print Os.listdir (path) #显示当前路径的文件目录和文件
Print Os.system (' ping www.baidu.com ') #ping to Baidu Speed

Here are a few additional ways to file and directory on the Python OS:
1. Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, it will also separate the last directory as the file name, and it will not determine if the file or directory exists

2. The Os.path.isfile () and Os.path.isdir () functions respectively verify that the given path is a file or a directory
3. Os.path.abspath (name): Get absolute path
4. Os.path.splitext (): Detach file name and extension
5. Os.path.join (path,name): Connection directory with file name or directory
6. Os.path.basename (PATH): Return file name
7. Os.path.dirname (PATH): Return file path

I'm building a file.py file to implement the above method:
file.py Code:
#encoding =utf-8
Import OS
Path = OS.GETCWD () #取得当前的路径, path content is:/home/haha/python
Print Os.path.split (path) #分割文件名与目录, my result is ('/home/haha ', ' python ')
dir = os.path.split (path) [0]
If Os.path.isdir (dir): #如果提供的是目录的话, the method returns True, otherwise the False,isfile () method is returned to determine if the file returns True, not the file will return false
Print dir, ' is a dir '
There are still a few methods, the reader can experiment with their own, so that the effect is better, familiar with the Python OS module has a lot of help!

Lao Wang Python offers Python books and hopes to be of help to everyone.


Original article please note reproduced from Lao Wang Python, this article address: http://www.cnpythoner.com/post/92.html

Author: Lao Wang @python python tutorial
Lao Wang Python, providing pythn related django tutorials and Python downloads, I hope you can enjoy!

A little bit of Python OS experience

Related Article

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.