標籤:建立 body 分離 getc ctime span shel 擷取檔案屬性 mtime
OS模組簡單介紹:
它是一個Python的系統編程的操作模組,可以處理檔案和目錄。比如尋找檔案或目錄,對大量的路徑與檔案處理。
常用操作方法:
os.name :指出當前你使用的操作平台,‘nt’代表window,‘posix’代表linux。
os.getcwd() :擷取當前工作路徑,即當前工作目錄的路徑。
os.listdir(path):列舉目錄下的全部檔案,返回結果是清單類型。
os.path.abspath(path):返回正常化的絕對路徑。
os.path.spilit(path):返迴路徑的目錄名和檔案名稱字,傳回型別為元組。
os.exit():終止當前進程。
os.system():運行shell命令。
os.sep 可以取代作業系統特定的路徑分割符。
os.linesep字串給出當前平台使用的行終止符。
>>> os.linesep‘\r\n‘ #Windows使用‘\r\n‘,Linux使用‘\n‘而Mac使用‘\r‘。>>> os.sep‘\\‘ #Windows
>>> os.system(‘dir‘)0>>> os.system(‘cmd‘) #啟動dos
檔案操作:
os.mknod(finename) 建立空檔案
os.stat(filename):擷取檔案屬性。
os.chmod(filename,mode)用於變更檔或目錄的許可權。
os.path.getsize(name):擷取檔案的大小
os.remove(filename):刪除某個檔案
os.path.getmtime(path):檔案或檔案夾的最後修改時間,從新紀元到訪問時的秒數。
os.path.getatime(path):檔案或檔案夾的最後訪問時間,從新紀元到訪問時的秒數。
os.path.getctime(path):檔案或檔案夾的建立時間,從新紀元到訪問時的秒數。
>>> os.path.getmtime(‘E:/python/byte-of-python-chinese-edition.pdf‘)1512013528.4031286>>> os.path.getatime(‘E:/python/byte-of-python-chinese-edition.pdf‘)1521650910.2861865>>> os.path.getctime(‘E:/python/byte-of-python-chinese-edition.pdf‘)1513432537.1275935>>>
目錄操作:
os.mkdir(dirname):建立目錄。
os.rmdir(dirname):刪除目錄。
os.removedirs(dirname):刪除多個目錄。
os.getcwd():擷取目前的目錄。
os.chdir(dirname):更改工作目錄為dirname。
路徑名操作:
os.path.isdir(name):判斷是否為目錄,True or False。
os.path.isfile(name):判斷是否為檔案,True or False。
os.path.getsize(name):擷取檔案的大小
os.path.abspath(name):擷取檔案的絕對路徑
os.path.split(name):分離目錄名和檔案名稱,傳回型別為元組。
os.path.splitext(name):分離檔案與副檔名,傳回型別為元組。
os.path.normpath(path):規範path字串形式。
os.path.join(dirname,filename):串連目錄名與檔案名稱。
os.path.basename(path):返迴文件名。
os.path.dirname(path):返迴文件目錄名。
>>> os.path.normpath(‘E:/python/byte-of-python-chinese-edition.pdf‘)
‘E:\\python\\byte-of-python-chinese-edition.pdf‘
>>> os.path.getsize(‘E:/python/byte-of-python-chinese-edition.pdf‘)2243165>>> os.path.split(‘E:/python/byte-of-python-chinese-edition.pdf‘)(‘E:/python‘, ‘byte-of-python-chinese-edition.pdf‘)>>> os.path.splitext(‘E:/python/byte-of-python-chinese-edition.pdf‘)(‘E:/python/byte-of-python-chinese-edition‘, ‘.pdf‘)>>> os.path.basename(‘E:/python/byte-of-python-chinese-edition.pdf‘)‘byte-of-python-chinese-edition.pdf‘>>> os.path.dirname(‘E:/python/byte-of-python-chinese-edition.pdf‘)‘E:/python‘
python之os模組分類整理