1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportOS4 5 __author__='Virtuous'6 #Get current directory7Currentpath =OS.GETCWD ()8 Print(Currentpath)9 #C:\Users\ Virtuous \pycharmprojects\pydemoTen One #returns all file and directory names under the specified directory A Print(Os.listdir (currentpath)) - #['. Idea ', ' dict.py ', ' file.py ', ' fonin_02.py ', - #' forin_02_v1.py ', ' format_01.py ', ' list_01.py ', ' list_02.py ', ' test.py ', ' zfc.py '] the - - #determine if a path is real -Path = Currentpath +"\ceshi.py" + ifos.path.exists (path): - #If there is a delete + #removedirs Delete Empty directory, if there is content can not delete directory A os.remove (path) at Else: - Print("file is not exist") - - #determines whether the given path is a file that returns a Boolean - Print(Os.path.isfile (currentpath+"/file.py")) - #determine if the given path is a directory in Print(Os.path.isdir (currentpath)) - #determine if it is an absolute path to Print(Os.path.isabs (currentpath)) + #determine if it is a link - Print(Os.path.islink ('http://www.baidu.com')) the #why return False? Http://www.baidu.com, is this not a connection? * $ Panax Notoginseng - #returns the directory name and file name of a path the Print(Os.path.split (currentpath)) + #(' c:\\users\\ virtuous \\PycharmProjects ', ' Pydemo ') A Print(Os.path.split (currentpath+"/file.py")) the #(' c:\\users\\ virtuous \\PycharmProjects\\PyDemo ', ' file.py ') + #(' c:\\users\\ virtuous \\PycharmProjects\\PyDemo ', ' file.py ') - $ #Detach Extension $ Print(Os.path.splitext (currentpath+"/file.py")) - #(' c:\\users\\ virtuous \\pycharmprojects\\pydemo/file ', '. Py ') - the #Get path name - Print(Os.path.dirname (currentpath))Wuyi #C:\Users\ Virtuous \pycharmprojects the Print(Os.path.dirname (currentpath+"/file.py")) - #C:\Users\ Virtuous \pycharmprojects\pydemo Wu - #Get file name About Print(Os.path.basename (currentpath)) $ #Pydemo - Print(Os.path.basename (currentpath+"/file.py")) - #file.py - A #get the system name for Windows, it's ' NT ', and for Linux/unix users, it's ' POSIX ' + Print(Os.name) the #NT - $ #Linux under command the #Read and SET environment variables: os.getenv () and os.putenv () the #gives the line terminator used by the current platform Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R ' the #windows and Linux have a different path, Windows is split with \ \, Linux is separated by A/, the #Os.sep automatically chooses which delimiter to use according to the system. - in # Renaming the #Enter directory First the Os.chdir (Currentpath) AboutOs.rename ("dict.py","dict1.py") theOs.renames ("dict1.py","dict2.py")
#Create a multilevel directory: Os.makedirs#os.makedirs (' d:\h\i\z\k ')#Create a single directory: Os.mkdirPrint(Os.stat (currentpath+"/file.py"))#Os.stat_result (st_mode=33206, st_ino=27866022694406647, st_dev=4238402480,#st_nlink=1, St_uid=0, St_gid=0, st_size=2740, st_atime=1481207487 , st_mtime=1481207487, st_ctime=1481204713)#modify file permissions and timestamps: Os.chmod (Path,mode) requires only 2 parameters, one is the path, and the other is the schema that describes the path.#Terminate Current process: Os.exit ()#Get File SizePrint(Os.path.getsize (currentpath+"/file.py"))#3102
。。。 http://www.runoob.com/python/os-file-methods.html This site in more detail ...
---python_os---python action file notes