Python OS experience and pythonos experience
A few days ago, I used a file replacement function.Python OSThe python OS module is very powerful,
This module is especially important if you want your python program to be unrelated to 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 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. py code:
# 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.
Original article please 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!