Python small white-day5 os module

Source: Internet
Author: User

OS Module

Provides an interface to invoke the operating system

12345678910111213141516171819202122232425262728293031 import osprint(‘getcwd()‘,os.getcwd()) #获取当前工作目录,即当前python脚本工作的目录路径#os.chdir("dirname")  #改变当前脚本工作目录;相当于shell下cdprint(‘curdir‘,os.curdir)  #返回当前目录: (‘.‘)print(‘pardir‘,os.pardir)  #获取当前目录的父目录字符串名:(‘..‘)#os.makedirs(‘dirname1/dirname2‘)    #可生成多层递归目录#os.removedirs(‘dirname1‘)    #若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推#os.mkdir(‘dirname‘)    #生成单级目录;相当于shell中mkdir dirname#os.rmdir(‘dirname‘)    #删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname#os.listdir(‘dirname‘)    #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印#os.remove()  #删除一个文件#os.rename("oldname","newname")  #重命名文件/目录print(‘stat()‘,os.stat(‘F:\PycharmProjects\s12\day5‘))  #获取文件/目录信息print(‘sep()‘,os.sep)    #输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"print(‘linesep()‘,os.linesep)    #输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"print(‘pathsep()‘,os.pathsep)    #输出用于分割文件路径的字符串print(‘name()‘,os.name)    #输出字符串指示当前使用平台。win->‘nt‘; Linux->‘posix‘#os.system("bash command")  #运行shell命令,直接显示print(‘environ()‘,os.environ)  #获取系统环境变量path = __file__print(‘path.abspath()‘,os.path.abspath(path))  #返回path规范化的绝对路径print(‘path.split()‘,os.path.split(path))  #将path分割成目录和文件名二元组返回print(‘path.dirname()‘,os.path.dirname(path))  #返回path的目录。其实就是os.path.split(path)的第一个元素print(‘path.basename()‘,os.path.basename(path))  #返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素print(‘path.exists()‘,os.path.exists(path))  #如果path存在,返回True;如果path不存在,返回Falseprint(‘path.isabs()‘,os.path.isabs(path)) #如果path是绝对路径,返回Trueprint(‘path.isfile()‘,os.path.isfile(path))  #如果path是一个存在的文件,返回True。否则返回Falseprint(‘path.isdir()‘,os.path.isdir(path))  #如果path是一个存在的目录,则返回True。否则返回False#os.path.join(path1[, path2[, ...]])  #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略print(‘path.getatime()‘,os.path.getatime(path))  #返回path所指向的文件或者目录的最后存取时间print(‘path.getmtime()‘,os.path.getmtime(path))  #返回path所指向的文件或者目录的最后修改时间

The output results are as follows:

1234567891011121314151617181920 getcwd() F:\PycharmProjects\s12\day5curdir .pardir ..stat() os.stat_result(st_mode=16895, st_ino=844424930132254, st_dev=57894, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1454509730, st_mtime=1454509730, st_ctime=1454128462)sep() \linesep() pathsep() ;name() ntenviron() environ({‘USERNAME‘: ‘Administrator‘, ‘PATH‘: ‘D:\\Program Files\\Python35\\Scripts\\;D:\\Program Files\\Python35\\;C:\\Program Files (x86)\\Common Files\\NetSarang;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;D:\\Program Files\\TortoiseSVN\\bin‘, ‘PROCESSOR_IDENTIFIER‘: ‘Intel64 Family 6 Model 94 Stepping 3, GenuineIntel‘, ‘HOMEPATH‘: ‘\\Users\\Administrator‘, ‘COMMONPROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)\\Common Files‘, ‘PYTHONUNBUFFERED‘: ‘1‘, ‘USERDOMAIN‘: ‘YOS-01601202250‘, ‘COMSPEC‘: ‘C:\\Windows\\system32\\cmd.exe‘, ‘NVIDIAWHITELISTED‘: ‘0x01‘, ‘NUMBER_OF_PROCESSORS‘: ‘4‘, ‘PROCESSOR_ARCHITECTURE‘: ‘AMD64‘, ‘PYCHARM_HOSTED‘: ‘1‘, ‘SYSTEMROOT‘: ‘C:\\Windows‘, ‘WINDOWS_TRACING_LOGFILE‘: ‘C:\\BVTBin\\Tests\\installpackage\\csilogfile.log‘, ‘LOGONSERVER‘: ‘\\\\YOS-01601202250‘, ‘SESSIONNAME‘: ‘Console‘, ‘PROGRAMW6432‘: ‘C:\\Program Files‘, ‘SHIM_MCCOMPAT‘: ‘0x810000001‘, ‘COMPUTERNAME‘: ‘YOS-01601202250‘, ‘PUBLIC‘: ‘C:\\Users\\Public‘, ‘PROCESSOR_LEVEL‘: ‘6‘, ‘PROGRAMFILES‘: ‘C:\\Program Files‘, ‘HOMEDRIVE‘: ‘C:‘, ‘PROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)‘, ‘OS‘: ‘Windows_NT‘, ‘ALLUSERSPROFILE‘: ‘C:\\ProgramData‘, ‘WINDIR‘: ‘C:\\Windows‘, ‘APPDATA‘: ‘C:\\Users\\Administrator\\AppData\\Roaming‘, ‘PSMODULEPATH‘: ‘C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\‘, ‘COMMONPROGRAMW6432‘: ‘C:\\Program Files\\Common Files‘, ‘PATHEXT‘: ‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW‘, ‘LOCALAPPDATA‘: ‘C:\\Users\\Administrator\\AppData\\Local‘, ‘PYTHONPATH‘: ‘F:\\PycharmProjects\\s12‘, ‘USERPROFILE‘: ‘C:\\Users\\Administrator‘, ‘COMMONPROGRAMFILES‘: ‘C:\\Program Files\\Common Files‘, ‘TEMP‘: ‘C:\\Users\\ADMINI~1\\AppData\\Local\\Temp‘, ‘SYSTEMDRIVE‘: ‘C:‘, ‘PYTHONIOENCODING‘: ‘UTF-8‘, ‘WINDOWS_TRACING_FLAGS‘: ‘3‘, ‘FP_NO_HOST_CHECK‘: ‘NO‘, ‘TMP‘: ‘C:\\Users\\ADMINI~1\\AppData\\Local\\Temp‘, ‘PROCESSOR_REVISION‘: ‘5e03‘, ‘PROGRAMDATA‘: ‘C:\\ProgramData‘})path.abspath() F:\PycharmProjects\s12\day5\os模块.pypath.split() (‘F:/PycharmProjects/s12/day5‘, ‘os模块.py‘)path.dirname() F:/PycharmProjects/s12/day5path.basename() os模块.pypath.exists() Truepath.isabs() Truepath.isfile() Truepath.isdir() Falsepath.getatime() 1454509730.168path.getmtime() 1454509730.168




From for notes (Wiz)

Python small white-day5 os module

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.