This is the 21st day of my participation in the August Text Challenge.More challenges in August

preface

Sanmao once sighed:

“What is the mind like? There is a labyrinth that stretches thousands of miles across. There is no boat that can ferry people. Throughout life, we are learning how to be our own ferrymen.

You can watch this blogger’s article about installation and LocalizationVsCode download, Installation and LocalizationAs well asPython series: windows10 configuration Python3.0 development environment!After installation, restart VsCode!


On! OS module

OS moduleMainly used for dealing with operating systems,PythonIn theOS moduleInclude universal OPERATING system functionality, that is, if you want your application to be platform independent, thenOS moduleIs particularly important. The method is as follows

  • Os.getcwd () gets the current working directory, the path of the current working Python script
  • Os.chdir (“dirname”) changes the current script working directory; Equivalent to shell CD
  • Os.curdir returns the current directory: (‘.’)
  • Os. pardir gets the parent directory of the current directory string name :(‘.. ‘)
  • Os.makedirs (‘dirname1/dirname2’) generates a multi-level recursive directory
  • Os.removedirs (‘dirname1’) if the directory is empty, delete it and recurse to the upper directory, if it is also empty, delete it, and so on
  • Os.mkdir (‘dirname’) generates a single-level directory; Equivalent to mkdir dirname in shell
  • Os.rmdir (‘dirname’) deletes a single-level empty directory. If the directory is not empty, it cannot be deleted and an error is reported. Equivalent to rmdir dirname in shell
  • Os.listdir (‘dirname’) lists all files and subdirectories under the specified directory, including hidden files, and prints them as a list
  • Os.remove () removes a file
  • Os.rename (“oldname”,”newname”) renames a file/directory
  • Os.stat (‘path/filename’) gets file/directory information
  • Os.sep Prints the operating system-specific path separator, “\” in Win and “/” in Linux.
  • Os.linesep prints the line terminator used by the current platform, “\t\n” for Win and “\n” for Linux
  • Os. pathsep Outputs the string used to split the file path. , under Linux is:
  • Os.name The output string indicates the platform currently in use. win->’nt’; Linux->’posix’
  • Os. system(“bash command”) Runs the shell command to display it directly
  • Os. environ Obtains system environment variables
  • Os.path. abspath(path) Returns the normalized absolute path of path
  • Os.path.split (path) Returns a binary split of path into directories and file names
  • Os.path. dirname(path) Returns the directory of path. This is the first element of os.path.split(path)
  • Os.path. basename(path) Returns the last file name of path. If path ends in/or \, then null is returned. That is the second element of os.path.split(path)
  • Os.path.exists (path) Returns True if path exists; If path does not exist, return False
  • Os.path. isabs(path) Returns True if path is an absolute path
  • Os.path. isfile(path) Returns True if path is an existing file. Otherwise return False
  • Os.path. isdir(path) Returns True if path is an existing directory. Otherwise return False
  • Os.path. join(path1[, path2[,…]]) returns after combining multiple paths. Parameters before the first absolute path are ignored
  • Os.path.getatime (path) Returns the last access time of the file or directory to which path points
  • Os.path.getmtime (path) Returns the last modification time of the file or directory to which path points

The following code is best to goCMD (command indicator)Try it

#! /usr/bin/env python
# -*- encoding: utf-8 -*-
@file: osdemo.py @time: 2019/10/21 10:34:18@author: YongJia Chen @version: 1.0 @contact: [email protected] @License : (C)Copyright 2018-2019, Liugroup-NLPR-CASIA @Desc : None '''

# here put the import lib

import os

Os.getcwd () gets the current working directory, the path of the current working Python script
os.getcwd()

#2, os.chdir("dirname") changes the current script working directory; Equivalent to shell CD
os.chdir(R "Specified directory address")
os.getcwd()

Os.makedirs ('dirname1/dirname2') can generate multiple recursive directories
os.makedirs(r"test1\test2")
Generate test1\test2 in the current directory

#4, os.removedirs('dirname1') if the directory is empty, delete it and recurse to the upper directory, if it is also empty, delete it, and so on
os.removedirs(r"test1\test2")
Delete test1\test2 from current directory

Os.mkdir ('dirname'); Equivalent to mkdir dirname in shell
os.listdir()

Os.rmdir ('dirname') delete a single empty directory. Equivalent to rmdir dirname in shell
os.mkdir(r"test1\test2")

Os.rmdir ('dirname') delete a single empty directory. Equivalent to rmdir dirname in shell
os.rmdir(r"test1")

#8, os.listdir('dirname') lists all files and subdirectories under the specified directory, including hidden files, and prints them as a list
os.listdir()

#9, os.remove() removes a file
os.rename(r"test1".r"test001")

Os.stat ('path/filename') get file/directory information
os.stat(r"test001")
os.stat_result(st_mode=16895,
               st_ino=21110623253299938,
               st_dev=1653245188,
               st_nlink=1,
               st_uid=0,
               st_gid=0,
               st_size=0,
               st_atime=1529501427,
               st_mtime=1529501427,
               st_ctime=1529499311)

#11, os.system("bash command"
os.system("ipconfig")

Os.path. abspath(path) Returns the normalized absolute path of path
os.path.abspath(r"test1/test2")

#13, os.path.split(path) splits path into directories and filenames
dir1 = os.path.abspath(r"test1/test2")
os.path.split(dir1)

Os.path. dirname(path) Returns the directory of path. This is the first element of os.path.split(path)
os.path.dirname(dir1)

Os.path. basename(path) Returns the last filename of path. If path ends in/or \, then null is returned. That is the second element of os.path.split(path)
os.path.basename(dir1)

#15, os.path.exists(path) Returns True if path exists; If path does not exist, return False
os.path.exists(dir1)

#16, os.path.join(path1[, path2[,...]]) join(path1[, path2[,...]]
os.path.join(R "specified directory address. Py".R "specified directory address. Py")

Os.path.getatime (path) returns the last access time of the file or directory to which path points
os.path.getatime(R "specified directory address. Py")

Os.path. getmTime (path) returns the last modification time of the file or directory to which path points
os.path.getmtime(R "specified directory address. Py")
Copy the code

OS moduleIt is aimed at operating the operating system indirectly. It is the interface between the program and the operating system, making the program independent of any operating system platform.


sysThe module

Sys moduleIs aimed atThe Python interpreterPerform related operations.

  • Sys. argv command line argument List, the first element is the program’s own path
  • Sys.exit (n) Exit the program, exit(0) on normal exit
  • Sys. version Gets the version information about the Python interpreter
  • Sys. maxint Maximum Int value
  • Sys. path returns the search path of the module, initialized using the value of the PYTHONPATH environment variable
  • Sys. platform Returns the platform name of the operating system

The following code is best to goCMD (command indicator)Try it

#! /usr/bin/env python
# -*- encoding: utf-8 -*-
@file: sysdemo.py @time: 2019/10/21 10:38:12 @author: YongJia Chen @version: 1.0 @contact: [email protected] @License : (C)Copyright 2018-2019, Liugroup-NLPR-CASIA @Desc : None '''

# here put the import lib

import sys, time
Sys. version Gets the version information for the Python interpreter
sys.version
# '3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]'

Sys. path returns the module's search path, initialized with the value of the PYTHONPATH environment variable
sys.path
# ['', 'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages', 'C: \ \ Users \ \ lenovo \ \ AppData \ \ Local \ \ designed \ \ \ \ Python Python37 \ \ lib \ \ site - packages \ \ beautifulsoup4-4.8.0 - py3.7. Egg'. 'C: \ \ Users \ \ lenovo \ \ AppData \ \ Local \ \ designed \ \ \ \ Python Python37 \ \ lib \ \ site - packages \ \ soupsieve - 1.9.4 - py3.7. Egg']

# the progress bar
for i in range(100):
    sys.stdout.write("$")  Write to cache first, print to screen once
    time.sleep(0.1)
    sys.stdout.flush()  # refresh the screen each time
Copy the code

On! A few extra words about PIP and PIP3

PIP and PIp3 are used to distinguish between installed versions if you have both python2 and Python3 installed

Import sys: Pip3 install sys is required

Import requests: pip3 install requests is required first

In the same way, if you need the desired package, go to >import XXX: pip3 install XXX

Go and have a look!


🎉 finally

  • For more references, see here:Chen Yongjia’s blog

  • Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!