os妯″潡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了os妯″潡相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/col' title='col'>col
鍛婅瘔 甯哥敤鏂规硶 鍒犻櫎鏂囦欢 efi os妯″潡 mtime 鍒犻櫎 linPython鐨勬爣鍑嗗簱涓殑os妯″潡涓昏娑夊強鏅亶鐨勬搷浣滅郴缁熷姛鑳姐€傚彲浠ュ湪Linux鍜學indows涓嬭繍琛岋紝涓庡钩鍙版棤鍏炽€?/p>
瀵煎叆妯″潡
import os
os妯″潡
os妯″潡甯哥敤鏂规硶
甯哥敤鏂囦欢鍜屾枃浠跺す鐨勬搷浣?/span>
- getcwd()
- 鑾峰彇褰撳墠宸ヤ綔鐩綍(褰撳墠宸ヤ綔鐩綍榛樿閮芥槸褰撳墠鏂囦欢鎵€鍦ㄧ殑鏂囦欢澶?
-
result = os.getcwd() print(result)
- chdir()
- 鏀瑰彉褰撳墠宸ヤ綔鐩綍
-
os.chdir(鈥?/span>/home/sy鈥?/span>) result = os.getcwd() print(result)
- listdir()
- 鑾峰彇鎸囧畾鏂囦欢澶瑰唴鎵€鏈夊唴瀹圭殑鍚嶇О鍒楄〃
-
result = os.listdir(鈥?/span>/home/sy鈥?/span>) print(result)
- mkdir()
- 鍒涘缓鏂囦欢澶?/li>
-
os.mkdir(鈥?/span>girls鈥?/span>)
- makedirs()
- 閫掑綊鍒涘缓鏂囦欢澶?/li>
-
os.makedirs(鈥?/span>/home/sy/a/b/c/d鈥?/span>)
- rmdir()
- 鍒犻櫎绌虹洰褰?/strong>
-
os.rmdir(鈥?/span>girls鈥?/span>)
- removedirs()
- 閫掑綊鍒犻櫎鏂囦欢澶癸紝蹇呴』閮芥槸绌虹洰褰?/strong>
-
os.removedirs(鈥?/span>/home/sy/a/b/c/d鈥?/span>)
- rename()
- 鏂囦欢鎴栬€呮枃浠跺す閲嶅懡鍚?/li>
-
os.rename(鈥?/span>/home/sy/a鈥?/span>,鈥?/span>/home/sy/alibaba鈥?/span> os.rename(鈥?/span>02.txt鈥?/span>,鈥?/span>002.txt鈥?/span>)
- stat()
- 鑾峰彇鏂囦欢鎴栬€呮枃浠跺す鐨勪俊鎭?/li>
-
result = os.stat(鈥?/span>/home/sy/PycharmProject/Python3/10.27/01.py) print(result)
- system()
- 鎵ц绯荤粺鍛戒护锛堝嵄闄╁嚱鏁帮級
-
result = os.stat(鈥?/span>/home/sy/PycharmProject/Python3/10.27/01.py) print(result)
甯哥敤鐜鍙橀噺鐨勬搷浣?/span>
鐜鍙橀噺鏄湪鎿嶄綔绯荤粺涓竴涓叿鏈夌壒瀹氬悕瀛楃殑瀵硅薄锛屽畠鍖呭惈浜嗕竴涓垨鑰呭涓簲鐢ㄧ▼搴忔墍灏嗕娇鐢ㄥ埌鐨勪俊鎭€備緥濡俉indows鍜孌OS鎿嶄綔绯荤粺涓殑path鐜鍙橀噺锛屽綋瑕佹眰绯荤粺杩愯涓€涓▼搴忚€屾病鏈夊憡璇夊畠绋嬪簭鎵€鍦ㄧ殑瀹屾暣璺緞鏃讹紝绯荤粺闄や簡鍦ㄥ綋鍓嶇洰褰曚笅闈㈠鎵炬绋嬪簭澶栵紝杩樺簲鍒皃ath涓寚瀹氱殑璺緞鍘绘壘銆傜敤鎴烽€氳繃璁剧疆鐜鍙橀噺锛屾潵鏇村ソ鐨勮繍琛岃繘绋嬨€?/p>
- getenv()
- 鑾峰彇绯荤粺鐨勭幆澧冨彉閲?/li>
-
result = os.getenv(鈥?/span>PATH鈥?/span>) print(result.split(鈥?/span>:鈥?/span>))
- putenv()
- 灏嗕竴涓洰褰曟坊鍔犲埌鐜鍙橀噺涓紙涓存椂澧炲姞锛屽彧瀵瑰綋鍓嶈剼鏈湁鏁堬級
-
os.putenv(鈥?/span>PATH鈥?/span>,鈥?/span>/home/sy/涓嬭浇鈥?/span>) os.system(鈥?/span>syls鈥?/span>)
鍏朵粬鏂规硶
- exit()
- 閫€鍑虹粓绔殑鍛戒护銆€銆€
os妯″潡涓父鐢ㄧ殑灞炴€?/span>
- curdir
- 琛ㄧず褰撳墠鏂囦欢澶癸紝浣嗘槸涓€鑸敤鈥?鈥樻潵琛ㄧず锛屼竴鑸儏鍐典笅鍙互鐪佺暐
-
print(os.curdir)
- pardir
- 琛ㄧず涓婂眰鏂囦欢澶癸紝浣嗘槸涓€鑸敤鈥?.鈥樻潵琛ㄧず锛屼笉鍙互鐪佺暐
-
print(os.pardir)
- name
- 鑾峰彇浠h〃鎿嶄綔绯荤粺鐨勫悕绉版垨鑰呭瓧绗︿覆
-
print(os.name) 銆€銆€#posix -> linux鎴栬€卽nix绯荤粺 nt -> window绯荤粺
- sep
- 鑾峰彇绯荤粺璺緞闂撮棿闅旂
-
print(os.sep) # window -> linux ->/
- extsep
- 鑾峰彇鏂囦欢鍚嶇О鍜屽悗缂€涔嬮棿鐨勯棿闅旂鍙?/li>
-
print(os.extsep) # window鍜宨nux -> .
- linesep
- 鑾峰彇鎿嶄綔绯荤粺鐨勬崲琛岀鍙?/li>
-
print(repr(os.linesep)) # window -> linux/unix ->
os.path瀛愭ā鍧?/span>
甯哥敤鏂规硶
- abspath()
- 灏嗙浉瀵硅矾寰勮浆鎹负缁濆璺緞
-
path = 鈥?/span>./boys鈥?/span>#鐩稿 result = os.path.abspath(path) print(result)
- dirname()
- 鑾峰彇瀹屾暣璺緞褰撲腑鐨勭洰褰曢儴鍒?/li>
-
path = 鈥?/span>/home/sy/boys鈥?/span> result = os.path.dirname(path) print(result) # /home/sy
- basename()
- 鑾峰彇瀹屾暣璺緞褰撲腑鐨勪富浣撻儴鍒?/li>
-
path = 鈥?/span>/home/sy/boys鈥?/span> result = os.path.basename(path) print(result) # boys
- split()
- 灏嗕竴涓畬鏁寸殑璺緞鍒囧壊鎴愮洰褰曢儴鍒嗗拰涓讳綋閮ㄥ垎
-
path = 鈥?/span>/home/sy/boys鈥?/span> result = os.path.split(path) print(result) # (鈥?home/sy鈥? 鈥榖oys鈥?
- join()
- 灏嗕袱涓矾寰勫悎骞舵垚涓€涓?/li>
-
var1 = 鈥?/span>/home/sy鈥?/span> var2 = 鈥?/span>000.py鈥?/span> result = os.path.join(var1,var2) print(result) # /home/sy 00.py , 浜х敓杩欎釜缁撴灉鐨勫師鍥犳槸锛屾垜鍦╳indow涓嬭繍琛岋紝鐢ㄧ殑鏄疞inux涓嬬殑鐩綍锛屼絾鏄嫾鎺ュ畠閲囩敤鐨勬槸褰撳墠绯荤粺榛樿鐨勮矾寰勯棿闂撮殧绗︼紝鑰寃indow榛樿鐨勬槸鈥榎鈥欍€?/span>
- splitext()
- 灏嗕竴涓矾寰勫垏鍓叉垚鏂囦欢鍚庣紑鍜屽叾浠栦袱涓儴鍒嗭紝涓昏鐢ㄤ簬鑾峰彇鏂囦欢鐨勫悗缂€
-
path = 鈥?/span>/home/sy/000.py鈥?/span> result = os.path.splitext(path) print(result) # (鈥?home/sy/000鈥? 鈥?py鈥?
- getsize()
- 鑾峰彇鏂囦欢鐨勫ぇ灏?/li>
-
path = 鈥?/span>./000.py鈥?/span> result = os.path.getsize(path) print(result) # 1981
- isfile()銆€銆€
- 妫€娴嬫槸鍚︽槸鏂囦欢
-
path = 鈥?/span>/home/sy/000.py鈥?/span> result = os.path.isfile(path) print(result)
- isdir()
- 妫€娴嬫槸鍚︽槸鏂囦欢澶?/li>
-
result = os.path.isdir(path) print(result)
- islink()
- 妫€娴嬫槸鍚︽槸涓€涓蒋閾炬帴
-
path = 鈥?/span>/initrd.img.old鈥?/span> result = os.path.islink(path) print(result)
- 鍏充簬鏂囦欢鐨勬搷浣滄椂闂?/strong>
-
#getctime() 鑾峰彇鏂囦欢鐨勫垱寤烘椂闂?get create time #getmtime() 鑾峰彇鏂囦欢鐨勪慨鏀规椂闂?get modify time #getatime() 鑾峰彇鏂囦欢鐨勮闂椂闂?get active time
-
import time filepath = 鈥?/span>/home/sy/涓嬭浇/chls鈥?/span> result = os.path.getctime(filepath) print(time.ctime(result)) result = os.path.getmtime(filepath) print(time.ctime(result)) result = os.path.getatime(filepath) print(time.ctime(result))
-
- exists()
- 妫€娴嬫煇涓矾寰勬槸鍚︾湡瀹炲瓨鍦?/li>
-
filepath = 鈥?/span>/home/sy/涓嬭浇/chls鈥?/span> result = os.path.exists(filepath) print(result)
- isabs()
- 妫€娴嬩竴涓矾寰勬槸鍚︽槸缁濆璺緞
-
path = 鈥?/span>/boys鈥?/span> result = os.path.isabs(path) print(result) # True
- samefile()
- 妫€娴嬩袱涓矾寰勬槸鍚︽寚鍚戞槸鍚屼竴涓枃浠?/li>
-
path1 = 鈥?/span>/home/sy/涓嬭浇/001鈥?/span> path2 = 鈥?/span>../../../涓嬭浇/001鈥?/span> result = os.path.samefile(path1,path2) print(result)
- ismount()
- 妫€鏌ユ寚瀹氬弬鏁版槸鍚︽槸鎸傝浇鐐?/li>
-
os.path.ismount("/mnt/cdrom")
甯哥敤灞炴€?/span>
- environ
- 鑾峰彇鍜岃缃郴缁熺幆澧冨彉閲忕殑鍐呯疆鍊?
- 鑾峰彇锛?鍜宱s涓殑os.getenv(鈥楶ATH鈥?鍚屾晥鏋?
-
print(os.environ[鈥?/span>PATH鈥?/span>])
-
- 璁剧疆
-
os.environ[鈥?/span>PATH鈥?/span>] += 鈥?/span>:/home/sy/涓嬭浇鈥?/span> os.system(鈥?/span>chls鈥?/span>)
-
- 鑾峰彇锛?鍜宱s涓殑os.getenv(鈥楶ATH鈥?鍚屾晥鏋?
- 鑾峰彇鍜岃缃郴缁熺幆澧冨彉閲忕殑鍐呯疆鍊?
以上是关于os妯″潡的主要内容,如果未能解决你的问题,请参考以下文章