os module and sys module literally see that this is related to the operating system, but the two functions are not the same focus, the former focusing on system operation, which focused operation window. Today to share relevant usage os module and sys modules (two modules in fact, there are ways to make common, article # internal standard number says not commonly used method )
os module
os method called directly
-
os.chdir ( '...') Return to the previous working directory
-
os.getcwd () to get the current working directory that the current directory path python script work
os.chdir ( "dirname") to change the current working directory script; cd shell at the equivalent of
changing the working directory:
import os
print(os.getcwd()) #输出当前的工作目录
os.chdir('test1') #改变当前的工作目录为test1这个目录,相当于shell下的cd
print(os.getcwd())
-
# os.curdir return to the current directory: ( '.')
# os.pardir to get the parent directory of the current directory string name :( '...') -
Generate and delete the directory (ie the directory folder)
os.makedirs ( 'dirname1 / dirname2')
may generate a multilayer recursive directory
os.removedirs ( 'dirname1')
If the directory is empty, delete, and recursively to the parent directory, if the parent is also empty, delete, and so on
os.mkdir ( 'dirname')
to generate a present level directory; it is equivalent to the shell mkdir dirname
os.rmdir ( 'dirname')
to delete an empty directory, if the directory is not empty can not be deleted, it will error; the equivalent of a shell rmdir dirname
-
Getting file information
os.listdir ( 'dirname') lists all the files and subdirectories in the specified directory (including hidden files), and by way of a list of print
os.stat ( 'path / filename')
get the file / directory information related information -
Delete and rename files
os.remove () deletes a file
os.rename ( "oldname", "newname ") to rename the file / directory -
Terminator and route into
os.linesep output current platform using line terminator, the windows system "\ r \ n", as "\ n" Linux system, the mac system \ R & lt
os.pathsep output for segmenting under string windows system file path is "\ r \ n", to "\ n" Linux system, the system is under mac \ r -
Delimiters and environment variables
# os.sep output operating system-specific path separator is "\", under Linux is "/" (the os.path.join a () a splice enough) the win
# The os.name It indicates the current output string using the internet. win -> 'NT'; Linux -> 'the POSIX'
# os.system ( "bash the Command") to run shell commands, direct display
# os.environ acquisition system environment variables (environment variables can be seen directly on the operating system)
os.path related method
- Directory path
os.path.abspath (path) Returns the path normalized absolute path
# Os.path.split (path) into the path directory and file name tuple returned (less), need to split time can generally be divided manually
os.path.dirname (path)
Returns the directory path of. In fact, os.path.split (path) of the first element
os.path.basename (path)
Returns the last path of the file name. How path to / \ or end, it will return a null value. That second element os.path.split (path) of
- The relevant judgment of the path and file
os.path.exists (path)
determines whether there is a path, if path exists, returns True; if the path does not exist, returns False
os.path.isabs (path)
to determine whether the absolute path, if path is an absolute path, returns True
os.path.isfile (path)
if the path is a file exists, returns True. Otherwise it returns False
os.path.isdir (path)
if the path is a directory exists, then return True. Otherwise it returns False
-
Stitching path
os.path.join (path1 [, path2 [,
...]]) stitching paths, a combination of a plurality of paths after the return, a parameter before the first absolute path will be ignored -
Modified View
os.path.getatime (path) Returns the file or directory path points to the last access time
os.path.getmtime (path) Returns the file or directory path points to the last modification time
sys module
- the sys.argv ()
a plurality of command line parameters List of parameters, the first path element of the program itself, may acquire the input
import sys
print(sys.argv) #输出当前的执行路径
-
sys.exit (n)
used to exit the program, when the normal exit exit (0) -
the sys.path ()
Returns the search path module using initialization value of the environment variable PYTHONPATH -
Access to information
# sys.version () to obtain the version of Python interpreter (less)
# sys.maxint () maximum value of Int
# sys.platform () returns the name of the operating system platform (less) -
sys.stdout.write ()
is the content stored in the cache, then a print on the display, a simple progress bar to achieve the following effects:
import sys,time
for i in range(10):
sys.stdout.write('#') #先将'#'放到缓存里面
time.sleep(1) #沉睡1秒
sys.stdout.flush() #再刷新一下
The result:
it will print to the screen one by one, just as the progress bar