我的Python成长之路---第六天---Python基础(18)---2016年2月20日(晴)
生活随笔
收集整理的這篇文章主要介紹了
我的Python成长之路---第六天---Python基础(18)---2016年2月20日(晴)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
os模塊
提供對操作系統進行調用的接口
>>> import os >>> os.getcwd() # 獲取當前工作目錄,類似linux的pwd命令 '/data/python/day5' >>> os.chdir('..') # 進入某個目錄,類似linux的cd命令 >>> os.getcwd() '/data/python' >>> os.curdir # 獲取當前目錄 '.' >>> os.pardir # 獲取當前目錄的父目錄 '..' >>> os.chdir('day5') >>> os.getcwd() '/data/python/day5' >>> os.makedirs('testdir1/testdir2') # 遞歸創建目錄相當于 mkdir -p命令 >>> os.makedirs('test_dir1/test_dir2') # 遞歸創建目錄相當于 mkdir -p命令 >>> os.listdir('.') # 顯示目錄下多所有文件 相當于linux的ls -a ['test_dir1'] >>> os.removedirs('test_dir1/test_dir2') # 刪除多級(遞歸)目錄,注意目錄必須是空的,若目錄為空刪除,并遞歸到上以及目錄,如果也為空則也刪除 >>> os.mkdir('test2') # 創建目錄,相當于mkdir >>> os.rmdir('test2') # 刪除目錄,相當于rm>>> f = open('test.txt', 'w') >>> f.write('testline') 8 >>> f.close() >>> os.listdir() ['testdir2', 'test.txt', 'testdir1'] >>> os.rename('test.txt', 'new_test.txt') #重命名 >>> os.stat('.') # 顯示目錄或文件的狀態,包括權限等 os.stat_result(st_mode=16877, st_ino=786731, st_dev=64784, st_nlink=4, st_uid=0, st_gid=0, st_size=4096, st_atime=1455695375, st_mtime=1455696066, st_ctime=1455696066) >>> os.sep # 獲取文件分割符,linux為/,windows為\\ '/' >>> os.name # 返回平臺名,linux為posix,win為nt 'posix' >>> os.linesep # 返回系統換行符,win下為\r\n '\n' >>> os.pathsep # 返回用于分割文件路徑的字符串,vin下為; ':' >>> os.system('ls') # 執行shell命令 testdir1 testdir2 0 >>> os.environ # 獲取系統環境變量 environ({'USER': 'root', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'SHELL': '/bin/bash', 'HOME': '/root', 'SHLVL': '1', 'HISTTIMEFORMAT': '% ...省略n多好... >>> os.path.abspath('.') # 返回目錄的絕對路徑 '/data/python/day5' >>> os.path.split('/data/python/day5') # 將path分割成目錄和文件,元祖返回 ('/data/python', 'day5') >>> os.path.dirname('/data/python/day5') # 返回path也即是split的第一個元素 '/data/python' >>> os.path.basename('/data/python/day5') # 返回文件名也即是split的第一個元素 'day5' >>> os.path.exists('/data/python/day5') # 判斷目錄或文件是否存在 True >>> os.path.isabs('/data/python/day5') # 判斷是否是絕對目錄,不考慮是否存在,說白了就是字符串符合絕對路徑的規范就返回True True >>> os.path.isabs('day5') False >>> os.path.isabs('/data/python/day6') # True >>> os.path.isfile('/data/python/day5') # 判斷是否是文件 False >>> os.path.isdir('/data/python/day5') # 判斷是否是目錄 True >>> os.path.isdir('/data/python/day6') False >>> os.path.join('/data/python/day6', 'test') # 組合目錄 '/data/python/day6/test' >>> os.path.getatime('/data/python/day5') # 返回文件或目錄的最后訪問時間 1455695375.9394312 >>> os.path.getmtime('/data/python/day5') # 返回文件或目錄的最后修改時間 1455696066.0034554 >>> os.path.getctime('/data/python/day5') # 返回文件或目錄的創建時間 1455696066.0034554轉載于:https://www.cnblogs.com/zhangxiaxuan/p/5195744.html
總結
以上是生活随笔為你收集整理的我的Python成长之路---第六天---Python基础(18)---2016年2月20日(晴)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ghost使用教程之系统备份与还原
- 下一篇: cms系统视频分享