os module of python (ipython, file, directory, permission, management)

Posted by vixtay on Fri, 24 Apr 2020 06:19:52 +0200

What is an os module

The os module provides the function interface functions of most operating systems. When the os module is imported, it will adapt to different operating system platforms and perform corresponding operations according to different platforms. When programming in python, it often deals with files and directories. At this time, the os module is indispensable. This section will give a detailed interpretation of the functions provided by the os module

1, Using scripts to automatically install Python versions

Requirement: no Python 3 system installed

If you have installed Python 3, you can only choose a different version to install

1. PyCharm connecting to Linux

2. os module executes shell command

The function of os.system():

Execute shell command
 Returns the return value of the shell command
 The output of the command is output to standard output

Code demonstration:


Script to install Python automatically

(1) Implementation steps:

Download Python source code
 Required dependency libraries for Python installation
 Compiling and installing Python

(2) Pseudo code:

1. Judge whether the user is root
 2. If yes, wait for the user to enter Python version
 3. Execute the shell command to download the source package
 4. Installation depends on development package
 5. Compile and install Python

(3) The script content is as follows (based on Python 2):


# coding=utf-8
import os

# Judge whether the user is root
if os.getuid() == 0:
    print('The current user is not root User!')

# Install Python dependency Library
cmd_module = 'yum -y install wget gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel'
res = os.system(cmd_module)
if res != 0:
    print('Python Dependency library installation failed, please execute the script again!')
    print('Python Dependency library installed successfully!')

# Enter Python version, Download Python source package to local directory
# weget url
version = raw_input('Please enter python Version: (3.6/3.8)')
if version == '3.6':
    url = 'https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tgz'
    url = 'https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz'
cmd = 'wget ' + url
res = os.system(cmd)
if res != 0:
    print('Python Source package download failed!')
    print('===============================>>>Python Source package download succeeded!')

# Unzip Python source package
# tar zxvf Python-3.8.2.tgz
if version == '3.6':
    package_name = 'Python-3.6.10'
    package_name = 'Python-3.8.1'
res = os.system('tar zxvf ' + package_name + '.tgz')
if res != 0:
    print('Decompression failed!')
    print('=============<<Decompression succeeded!>>===============')

# Necessary configuration, otherwise error: "make: *** [pybuilddir.txt] error 1"
# export LANG=zh_CN.UTF-8
# export LANGUAGE=zhb_CN.UTF-8
cmd_export_lang = 'export LANG=zh_CN.UTF-8'
cmd_export_language = 'export LANGUAGE=zhb_CN.UTF-8'
res1 = os.system(cmd_export_lang)
res2 = os.system(cmd_export_language)
if res1 != 0 or res2 != 0:
    print('Configuration failed, please check the script before running!')

# Switch Python directory
os.system('./configure --prefix=/usr/local/python3')
res = os.system('make && make install')
if res != 0:
    print('Source compilation failed!')
    print('=========<<Python Installation succeeded, please verify!>>==========')

# Modify user environment variables
os.system('echo "export PYTHON3=/usr/local/python3" >>~/.bash_profile')
os.system('echo "export PATH=$PYTHON3/bin:$PATH" >>~/.bash_profile')
os.system("source ~/.bash_profile")
os.system('cat ~/.bash_profile')
print('User environment variable has been changed, please verify!')
os.system('ln -s /usr/local/python3/bin/*  /usr/local/bin')
os.system('python3 --version')

3. Upload to Linux and execute

Linux execution

[root@python ~]# cd /opt/
[root@python opt]# python  test1.py

2, Python's os module shell

Python's os module encapsulates common file and directory operations. This article only lists some common methods, and more methods can be viewed Official documents.

Here are some common uses:

Method Explain
os.mkdir Create directory
os.rmdir Delete directory
os.rename rename
os.remove Delete file
os.getcwd Get current working path
os.walk Traverse directory
os.path.join Connection directory and file name
os.path.split Split file name and directory
os.path.abspath Get absolute path
os.path.dirname Get path
os.path.basename Get file or folder name
os.path.splitext Separating file names from extensions
os.path.isfile Determine whether the given path is a file
os.path.isdir Determine whether the given path is a directory

1. Install ipython

[root@root ~]# pip3 install -i https://pypi.douban.com/simple/ ipython

Launch ipython

[root@root ~]# ipython

If it cannot be started:

python3 -m IPython --version

To view the version of ipython, if you see the version information, you can use the python -m IPython command to start ipython

If you want to use the ipython command to start, you can add the following in the. Bash "profile under the user directory:

alias ipython="python3 -m IPython"

Launch ipython again

python3 -m IPython --version

That's it

2. Examples

The following directory structure is used as a reference for the following examples, and the working directory is / Users/ethan/coding/python.

└── coding
    └── python
        ├── hello.py    - file
        └── web         - Catalog

Take an example:

(1) os.path.abspath: get the absolute path of a file or directory

$ pwd
$ python
>>> import os                          # Remember to import the os module
>>> os.path.abspath('hello.py')
>>> os.path.abspath('web')
>>> os.path.abspath('.')                # Absolute path to the current directory

(2) os.path.dirname: get the path of the file or folder

>>> os.path.dirname('/Users/ethan/coding/python/hello.py')
>>> os.path.dirname('/Users/ethan/coding/python/')
>>> os.path.dirname('/Users/ethan/coding/python')

(3) os.path.basename: get file name or folder name

>>> os.path.basename('/Users/ethan/coding/python/hello.py')
>>> os.path.basename('/Users/ethan/coding/python/')
>>> os.path.basename('/Users/ethan/coding/python')

(4) os.path.splitext: separating file names from extensions

>>> os.path.splitext('/Users/ethan/coding/python/hello.py')
('/Users/ethan/coding/python/hello', '.py')
>>> os.path.splitext('/Users/ethan/coding/python')
('/Users/ethan/coding/python', '')
>>> os.path.splitext('/Users/ethan/coding/python/')
('/Users/ethan/coding/python/', '')

(5) os.path.split: separating directory and filename

>>> os.path.split('/Users/ethan/coding/python/hello.py')
('/Users/ethan/coding/python', 'hello.py')
>>> os.path.split('/Users/ethan/coding/python/')
('/Users/ethan/coding/python', '')
>>> os.path.split('/Users/ethan/coding/python')
('/Users/ethan/coding', 'python')

(6) os.path.isfile: is it a file

os.path.isdir: is it a directory

>>> os.path.isfile('/Users/ethan/coding/python/hello.py')
>>> os.path.isdir('/Users/ethan/coding/python/')
>>> os.path.isdir('/Users/ethan/coding/python')
>>> os.path.isdir('/Users/ethan/coding/python/hello.py')

(7) os.walk: traverse directory

os.walk is a common module for traversing directories. It returns a primitive ancestor containing three elements: (dirpath, dirnames, filenames). Dirpath is to return all the absolute paths in the directory in the form of string; dirnames is to return the folder names in each absolute path in the form of list list; filesnames is to return all the file names in the path in the form of list.

>>> for root, dirs, files in os.walk('/Users/ethan/coding'):
...     print root
...     print dirs
...     print files

3. os module open file

The method is as follows:

os.open(filename, flag, [,mode])

flag Parameter Description:

file # Files to open
flags # This parameter can be the following options, multiple separated by "|"
os.O_RDONLY # Open as read-only
os.O_WRONLY # Open in write only mode
os.O_RDWR # Open read-write
os.O_NONBLOCK # Open without blocking
os.O_APPEND # Open as append
os.O_CREAT # Create and open a new file
os.O_TRUNC # Open a file and truncate it to zero length (must have write permission)
os.O_EXCL # Returns an error if the specified file exists
os.O_SHLOCK # Get shared lock automatically
os.O_EXLOCK # Acquire independent lock automatically
os.O_DIRECT # Eliminate or reduce cache effects
os.O_FSYNC # Synchronous write
os.O_NOFOLLOW# Don't track soft links
mode # Similar to chmod().

4. os module operates on files

Common methods are as follows:

# read file
os.read(fd, buffersize)
# write file
os.write(fd, string)
# File pointer operation
os.lseek(fd, pos, how)
# Close file
Code demonstration:

File creation and writing

import os

# Open file
fd = os.open("abc.txt", os.O_RDWR | os.O_CREAT)

# Write string
str = "Hello Python!"
ret = os.write(fd, bytes(str, 'UTF-8'))

# Enter return value
print("The number of bits written is: ")

print("Write succeeded")

# Close file
print("File closed successfully!!")
The output results are as follows:
The number of bits written is: 
 Write succeeded
 File closed successfully!!

File read

import os

# Open file
fd = os.open("abc.txt", os.O_RDWR)

# Read text
ret = os.read(fd, 6)

# Close file
print("File closed successfully!!")
The output results are as follows:
b'Hello '
File closed successfully!!

5. os module management files and directories

Common methods are as follows:

os method Explain
remove(path) Delete file
rename(old, new) Modify the file or directory name
getcwd() Get current directory
listdir(path) Returns a list of all files in the current directory
mkdir(path [,mode]) Create directory
makedirs(path [,mode]) Create a multi-level directory
rmdir(path) Delete directory (directory must be empty)
removedirs(path) Delete multi-level directory (directory must be empty)
Code demonstration:
# coding=utf-8

import os

print(os.getcwd())  # pwd
print(os.listdir()) # ls
os.rename('abc.txt','test.txt') # mv abc.txt test.txt
os.remove('read.py')    # rm -f abc.txt

os.mkdir('test')    # mkdir dir1
os.makedirs('demo/abc') # mkdir -p dir2/dir22
os.rmdir('test')    # Directory must be empty
os.removedirs('demo')   #Directory must be empty

6. os module management file permission

os method Explain
access(path, mode) Judge the file permission: F ﹣ OK exists; < br > permission: R ﹣ OK, w ﹣ OK, X ﹣ OK
chmod(path, mode) Modify file permission: 0o755
chown(path, uid, gid) Change the file owner to - 1 if not modified
Code demonstration:
import os

# Test whether the path exists: OS. F ﹣ OK
res = os.access('test.txt',os.F_OK)

# Test whether the current user has read permission to the file
res = os.access('test.txt',os.R_OK)

# Test whether the current user has write permission to the file
res = os.access('test.txt',os.W_OK)

# Test whether the current user has permission to execute the file
res = os.access('test.txt',os.X_OK)

# Change the permissions of the current user

# Change the owner of the file
os.chown('test.txt', 1001, 1002)  

The premise of implementation is to ensure the existence of required documents.

7. os.path module manages files and paths

(1) Split path
os.path method Explain
split Returns a binary containing the path and filename of the file
dirname Return path to file
basename Return file name
splitext Returns a binary of the file extension and the part of the extension removed
Code demonstration:
In [10]: os.getcwd()
Out[10]: '/opt/os_demo'

In [11]: os.listdir()
Out[11]: ['os_access.py', 'test.txt']

In [12]: path = '/opt/os_demo/test.txt'

In [13]: os.path.split(path)
Out[13]: ('/opt/os_demo', 'test.txt')

In [14]: os.path.dirname(path)
Out[14]: '/opt/os_demo'

In [15]: os.path.basename(path)
Out[15]: 'test.txt'

In [16]: os.path.splitext(path) 
Out[16]: ('/opt/os_demo/test', '.txt')
(2) Build path
os.path method Explain
expanduser Expand the user's HOME directory, such as ~, ~ oracle
abspath Get the absolute path of the file or path
join Use different path separators to splice paths according to different operating system platforms
isabs Check whether a path is an absolute path
Code demonstration:
In [19]: os.path.expanduser('~') 
Out[19]: '/root'

In [20]: os.path.expanduser('~oracle') 
Out[20]: '/home/oracle'

In [21]: os.path.expanduser('~accp')
Out[21]: '/home/accp'

In [22]: os.path.expanduser('~acp') 
Out[22]: '~acp'

In [23]: os.path.abspath('.')
Out[23]: '/opt/os_demo'

In [24]: os.path.abspath('..')
Out[24]: '/opt'

In [25]: os.path.join('/opt/os_demo','test.txt')
Out[25]: '/opt/os_demo/test.txt'

In [26]: os.path.isabs('/opt/os_demo/') 
Out[26]: True

In [27]: os.path.isabs('.') 
Out[27]: False
(3) Get file properties
os.path Method
os.path.getmtime(path) Return to the latest file modification time
os.path.getctime(path) Return file path creation time
os.path.getsize(path) Returns the file size, or an error if the file does not exist
Code demonstration:
In [33]: os.path.getatime(path)
Out[33]: 1587547270.7306058

In [34]: os.path.getmtime(path)
Out[34]: 1587547270.7306058

In [35]: os.path.getctime(path)
Out[35]: 1587548055.4721448

In [36]: os.path.getsize(path)
Out[36]: 0
(4) Judge document type
os.path method Explain
os.path.isfile(path) Determine whether the path is a file
os.path.isdir(path) Determine whether the path is a directory
os.path.islink(path) Determine whether the path is a link
os.path.ismount(path) Determine whether the path is a mount point
Code demonstration:
In [37]: os.path.isfile(path)
Out[37]: True

In [38]: os.path.isdir(path)
Out[38]: False

In [39]: os.path.islink(path)
Out[39]: False

In [40]: os.path.ismount(path)
Out[40]: False

Topics: Python IPython shell Linux