Python3.5內(nèi)置模塊之os模塊、sys模塊、shutil模塊用法實例分析
本文實例講述了Python3.5內(nèi)置模塊之os模塊、sys模塊、shutil模塊用法。分享給大家供大家參考,具體如下:
1、os模塊:提供對操作系統(tǒng)進行調(diào)用的接口
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import os
print(os.getcwd()) #獲取當前的操作目錄,即當前Python腳本工作的目錄路徑
#os.chdir("F:\\PythonCode\\day5\\test") #改變當前腳本工作目錄,相當于shell下的cd
os.chdir(r"F:\PythonCode\day5\test") #與上面一句等價(推薦使用)
print(os.getcwd())
print(os.curdir) #返回當前目錄 '.'
print(os.pardir) #獲取當前目錄的父目錄字符串名 '..'
os.makedirs(r"F:\a\b\c") #生成多層遞歸目錄
#若目錄為空,則刪除,并遞歸到上一級目錄,如若也為空,則刪除,依此類推
os.removedirs(r"F:\a\b\c") #清理空文件夾
os.mkdir(r"F:\PythonCode\day5\t") #生成單級目錄,相當于shell中mkdir filename
os.rmdir(r"F:\PythonCode\day5\t") #刪除單級空目錄,若目錄不為空,無法刪除或報錯;相當于shell中rmdir filename
print(os.listdir(r"F:\PythonCode\day5\test")) #列出指定目錄下的所有文件和子目錄,包括隱藏文件,并以列表方式打印
os.remove(r"F:\PythonCode\day5\test\1.py") #刪除一個文件
os.rename(r"F:\PythonCode\day5\test\1.py",r"F:\PythonCode\day5\test\2.py") #重命名文件/目錄
print(os.stat(r"F:\PythonCode\day5\test")) #獲取文件/目錄信息
print(os.sep) #輸出操作系統(tǒng)特定的路徑分隔符,win下為"\\",Linux下為"/"
print(os.linesep) #輸出當前平臺使用的行終止符,win下為"\r\n",Linux下為"\n"
print(os.pathsep) #輸出用于分割文件路徑的字符串,win下為";",Linux下為":"
print(os.environ) #查看系統(tǒng)的環(huán)境變量
print(os.name) #輸出字符串指示當前使用平臺。win->'nt'; Linux->'posix'
print(os.system("dir")) #運行shell命令,直接顯示
print(os.path.abspath(r"F:\PythonCode\day5")) #返回path規(guī)范化的絕對路徑
print(os.path.split(r"F:\PythonCode\day5\test\1.py")) #將path分割成目錄和文件名二元組返回
print(os.path.dirname(r"F:\PythonCode\day5")) #返回path的目錄。其實就是os.path.split(path)的第一個元素
print(os.path.basename(r"F:\PythonCode\day5")) #返回path最后的文件名。如何path以/或\結(jié)尾,那么就會返回空值。
print(os.path.exists(r"F:\PythonCode\day5")) #如果path存在,返回True;如果path不存在,返回False
print(os.path.isabs(r"F:\PythonCode\day5")) #如果path是絕對路徑,返回True
print(os.path.isfile(r"F:\PythonCode\day5\p_test.py")) #如果path是一個存在的文件,返回True,否則返回False
print(os.path.isdir(r"F:\PythonCode\day5")) #如果path是一個存在的目錄,則返回True。否則返回False
print(os.path.join(r"F:",r"\PythonCode",r"\day5",r"\day")) #將多個路徑組合后返回,第一個絕對路徑之前的參數(shù)將被忽略
print(os.path.getatime(r"F:\PythonCode\day5")) #返回path所指向的文件或者目錄的最后存取時間
print(os.path.getmtime(r"F:\PythonCode\day5")) #返回path所指向的文件或者目錄的最后修改時間
運行結(jié)果:
F:\PythonCode\day5
F:\PythonCode\day5\test
.
..
['2.py', 'module_test.py', 'test.py']
os.stat_result(st_mode=16895, st_ino=8162774324625191, st_dev=104211, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1506609480, st_mtime=1506609480, st_ctime=1506579769)
\
;
environ({'PROCESSOR_LEVEL': '6', 'WINDOWS_TRACING_LOGFILE': 'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log', 'PROCESSOR_ARCHITECTURE': 'x86')
nt
������ F �еľ��� ѧϰ��
�������� 0001-9713
F:\PythonCode\day5\test ��Ŀ¼
2017/09/28 22:38 <DIR> .
2017/09/28 22:38 <DIR> ..
2017/09/28 22:37 69 2.py
2017/09/28 14:31 121 module_test.py
2017/09/28 14:35 237 test.py
3 ���ļ� 427 �ֽ�
2 ��Ŀ¼ 14,051,733,504 �����ֽ�
0
F:\PythonCode\day5
('F:\\PythonCode\\day5\\test', '1.py')
F:\PythonCode
day5
True
True
True
True
F:\day
1506656912.210523
1506656912.210523
2、sys模塊
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import sys
print(sys.argv) #命令行參數(shù)List,第一個元素是程序本身路徑
print(sys.version) #獲取Python解釋程序的版本信息
print(sys.path) #返回模塊的搜索路徑,初始化時使用PYTHONPATH環(huán)境變量的值
print(sys.platform) #返回操作系統(tǒng)平臺名稱
sys.stdout.write('please:') #標準輸出,寫入字符串輸出到屏幕
val = sys.stdin.readline()[:-1] #標準輸入
print(val)
sys.exit(0) #退出程序,正常退出時exit(0)
運行結(jié)果:
['F:/PythonCode/day5/sys_module.py'] 3.5.2 |Anaconda 4.2.0 (32-bit)| (default, Jul 5 2016, 11:45:57) [MSC v.1900 32 bit (Intel)] ['F:\\PythonCode\\day5', 'F:\\PythonCode', 'C:\\Users\\Administrator\\Anaconda3\\python35.zip', 'C:\\Users\\Administrator\\Anaconda3\\DLLs', 'C:\\Users\\Administrator\\Anaconda3\\lib', 'C:\\Users\\Administrator\\Anaconda3', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\Sphinx-1.4.6-py3.5.egg', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\setuptools-27.2.0-py3.5.egg'] win32 hello please:hello
3、shutil模塊:高級的文件、文件夾、壓縮包處理模塊
(1)將文件內(nèi)容拷貝到另一個文件中,可以部分內(nèi)容——shutil.copyfileobj(fsrc, fdst[, length])
f1 = open("p_test.py",encoding="utf-8")
f2 = open("p1.py","w",encoding="utf-8")
#將文件p_test.py內(nèi)容拷貝到另一個文件p1.py中,可以部分內(nèi)容
shutil.copyfileobj(f1,f2)
運行結(jié)果:

(2)拷貝文件——shutil.copyfile(src, dst)
import shutil
shutil.copyfile("p1.py","p2.py") #拷貝文件
運行結(jié)果:

(3)僅拷貝權(quán)限(內(nèi)容、組、用戶均不變)——shutil.copymode(src, dst)
(4)拷貝狀態(tài)的信息,包括:mode bits, atime, mtime, flags——shutil.copystat(src, dst)
(5)拷貝文件和權(quán)限——shutil.copy(src, dst)
(6)拷貝文件和狀態(tài)信息——shutil.copy2(src, dst)
(7)遞歸的去拷貝文件:
shutil.ignore_patterns(*patterns) shutil.copytree(src, dst, symlinks=False, ignore=None)
import shutil
shutil.copytree("test","test1") #遞歸的去拷貝文件
運行結(jié)果:

(8)遞歸的去刪除文件——shutil.rmtree(path[, ignore_errors[, onerror]])
(9)遞歸的去移動文件——shutil.move(src, dst)
(10)創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar——shutil.make_archive(base_name, format,...)
- base_name: 壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時,則保存至當前目錄,否則保存至指定路徑,
如:www =>保存至當前路徑
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/ - format: 壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
- root_dir: 要壓縮的文件夾路徑(默認當前目錄)
- owner: 用戶,默認當前用戶
- group: 組,默認當前組
- logger: 用于記錄日志,通常是logging.Logger對象
import shutil
shutil.make_archive("shutil_archive_test","zip","F:\PythonCode\day5")
運行結(jié)果: 
總結(jié):shutil 對壓縮包的處理是調(diào)用 ZipFile 和 TarFile 兩個模塊來進行的
import zipfile
z = zipfile.ZipFile("day5.zip","w")
z.write("p1.py")
print("===========")
z.write("p2.py")
z.close()
運行結(jié)果:
===========

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python?創(chuàng)建或讀取?Excel?文件的操作代碼
Excel是一種常用的電子表格軟件,廣泛應用于金融、商業(yè)和教育等領域,本文介紹Python?創(chuàng)建或讀取?Excel?文件的操作代碼,感興趣的朋友一起看看吧2023-09-09
Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù)
這篇文章主要介紹了Python爬蟲之使用BeautifulSoup和Requests抓取網(wǎng)頁數(shù)據(jù),本篇文章將介紹如何使用 Python 編寫一個簡單的網(wǎng)絡爬蟲,從網(wǎng)頁中提取有用的數(shù)據(jù),需要的朋友可以參考下2023-04-04
Python+PyQt5實現(xiàn)開發(fā)Memcached客戶端
這篇文章主要介紹了如何使用Python和PyQt5來制作一個Memcached客戶端,以便我們可以輕松地與Memcached服務器進行交互,感興趣的小伙伴可以了解一下2023-06-06
Pyspark讀取parquet數(shù)據(jù)過程解析
這篇文章主要介紹了pyspark讀取parquet數(shù)據(jù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
python中的property及屬性與特性之間的優(yōu)先權(quán)
這篇文章主要介紹了python中的property及屬性與特性之間的優(yōu)先權(quán),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
Pytorch使用MNIST數(shù)據(jù)集實現(xiàn)基礎GAN和DCGAN詳解
今天小編就為大家分享一篇Pytorch使用MNIST數(shù)據(jù)集實現(xiàn)基礎GAN和DCGAN詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
PyCharm利用pydevd-pycharm實現(xiàn)Python遠程調(diào)試的詳細過程
這篇文章主要介紹了PyCharm利用pydevd-pycharm實現(xiàn)Python遠程調(diào)試,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09

