Python Learning _os Module use

Source: Internet
Author: User
Tags python script

1 #!/usr/bin/python2 #-*-encoding:utf-8-*-3 #___author___: Oracle4 #___date____: 2018/5/17 a.m. 09:045 #___file_name:os Module6 #___project_name:python_lesson7 8 ImportSys,time,os9 " "OS module interaction between the program and the operating system provides access to the underlying interface of the operating systemTen The sys module acts as an interaction between the Python interpreter and the program, providing a range of functions and variables to manipulate the environment of the Python runtime One " " A " "print out the current directory OS.GETCWD ()" " - Print(OS.GETCWD ())#C:\Users\oracle\PycharmProjects\Python_Lesson\ Basic Article -  the " "Change current directory Os.chdir (' Other_dirname ')" " - Print(Os.chdir (R'C:\Users'))#None - Print(OS.GETCWD ())#C:\Users - #Create a 1.txt file under the current directory C:\Users +Open'1.txt','W', encoding='Utf-8') - " "list all files in the current directory Os.listdir ()" " + Print(Os.listdir ()) A #[' 1.txt ', ' All Users ', ' Default ', ' Default User ', ' Desktop.ini ', ' Oracle ', ' Public '] at  - #' Rename File/directory Os.rename (' Ole_name ', ' new_name ') ' - #os.rename (' 111.py ', ' How to use the OS module ') -  - " "returns the current directory (.)" " -Os.curdir#TypeError: ' str ' object is not callable, string objects are not callable, CurDir methods are not (), can be removed in " "Gets the parent directory string name of the current directory (.. )" " - Os.pardir to  + " "create multi-level catalogs at once Os.makedirs (' Path/path1/path2. ')" " - #os.makedirs ("b:/www/xxx/yyy/aaa") the  * " "os.removedirs (' dirnames ') cannot be deleted by recursively deleting empty directories starting from subdirectories, such as directory not empty" " $ #create a file in XXXPanax NotoginsengOpen (R'B:\www\xxx\file.txt','W') - #os.removedirs (R ' b:\www\xxx\yyy ') #OSError: [Winerror 145] The catalogue is not empty. : ' b:\\www\\xxx\\yyy ', because there are files and AAA directories under YYY the #os.removedirs (R ' b:\www\xxx\yyy\aaa ') #只删除了yyy和aaa目录 because there are files in XXX +  A " "Create directory Os.mkdir (' Path\dirname ')" " the Print(OS.GETCWD ())#C:\Users +Os.mkdir ('Python_study') - Print(Os.listdir (OS.GETCWD ())) $['1.txt','All Users','Default','Default User','Desktop.ini','Oracle',' Public','Python_study'] $  - " "Delete a single-level directory, not empty, cannot be deleted, Os.rmkdir (' \path\dirname ')" " -Os.rmdir ('b:\www\\xxx\yyy\\aaa')#The AAA directory is deleted the  - " "Delete a file, cannot delete folder Os.remove ()" "Wuyi Print(Os.listdir (OS.GETCWD ()))#View current directory, delete files 1.txt and directory Python_study theOs.remove (R'C:\users\python_study')#permissionerror: [Winerror 5] access is denied. : ' C:\\users\\python_study ' -Os.remove (R'C:\users\1.txt') Wu  - " "Delete a non-empty directory, use the method Shutil.rmtree (' Path\\filename ') in the module Shutil" " About ImportShutil $Shutil.rmtree (R'C:\users\python_study')#Delete Empty directory -Os.chdir ('b:\www')#switch to the WWW directory of the B-drive -Shutil.rmtree ('b:\www\\xxx\yyy') - Print(Os.listdir (OS.GETCWD ()))#[' xxx '] A  + " "Get file Information" " the Print(Os.stat ('b:\www')) - #>>>os.stat_result (st_mode=16895, st_ino=1125899906888842,\ $ #st_dev=1153046077, St_nlink=1, St_uid=0, st_gid=0, st_size=0,\ the #st_atime=1526540494, st_mtime=1526540494, st_ctime=1526540494) the " "output operating system-specific path delimiter, Windows is ' \ \ ', Linux is '/'" " the Print(OS.SEP)#' \ \ ' theReprint Cattle man Finishing: http://www.cnblogs.com/alex3714/articles/5161349. html -  in OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work the  theOs.chdir ("dirname") Change the current script working directory, equivalent to the shell CD AboutOs.curdir returns the current directory: ('.') theOs.pardir Gets the parent directory string name for the current directory: ('..') theOs.makedirs ('dirname1/dirname2') to generate a multi-level recursive directory theOs.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on +Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir dirname -Os.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir dirname theOs.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints as a listBayi os.remove () delete a file theOs.rename ("oldname","newname") Rename File/Catalogue theOs.stat ('Path/filename') Get File/directory information -OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/" -OS.LINESEP output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n" the os.pathsep output string for splitting the file path theThe Os.name output string indicates the current usage platform. Win->'NT'; Linux->'POSIX' theOs.system ("Bash Command") to run the shell command to display directly the Os.environ getting system environment variables - Os.path.abspath (path) returns the absolute path normalized by path the os.path.split (path) splits path into directory and file name two tuples returned the os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path) the os.path.basename (path) returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path)94 os.path.exists (path) returns true if path exists, false if path does not exist the os.path.isabs (path) returns True if path is an absolute path the os.path.isfile (path) returns True if path is a file that exists. Otherwise returns false the os.path.isdir (path) returns True if path is a directory that exists. Otherwise returns false98 Os.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored About os.path.getatime (path) returns the last access time of the file or directory to which path is pointing -Os.path.getmtime (Path) returns the last modified time of the file or directory to which path is pointing

Python Learning _os Module use

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.