Python目錄和文件處理總結詳解
更新時間:2019年09月02日 10:14:41 作者:gdjlc
這篇文章主要介紹了Python目錄和文件處理總結詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1、判斷目錄是否存在、判斷文件是否存在、創(chuàng)建目錄、重命名目錄或文件
import os
#獲取當前目錄路徑: E:\Work\Projects\python
print(os.getcwd())
#判斷當前目錄是否存在,不存在則創(chuàng)建目錄dir1
if not os.path.isdir('dir1'):
os.mkdir('dir1')
#判斷指定目錄是否存在,不存在則創(chuàng)建目錄dir22
if not os.path.isdir('E:\Work\Projects\python\dir22'):
os.mkdir('E:\Work\Projects\python\dir22')
#判斷當前目錄的指定文件是否存在
print(os.path.isfile('file1.txt'))
#重命名目錄dir22為dir2(rename也可用于文件)
os.rename('dir22', 'dir2')
#切換到目錄dir2
os.chdir('dir2')
#切換到目錄dir2后獲取當前目錄路徑: E:\Work\Projects\python\dir2
print(os.getcwd())
2、目錄和文件的查詢
當前目錄結構為
│ 1.txt │ 2.txt │ test1.py │ ├─dir1 │ dir1_1.txt │ dir1_2.txt │ └─dir2
(1)獲取指定目錄下的目錄和文件列表(不包括子目錄)
import os path = os.getcwd() for filename in os.listdir(path): print(os.path.join(path,filename)) '''運行結果: E:\Work\Projects\python\1.txt E:\Work\Projects\python\2.txt E:\Work\Projects\python\dir1 E:\Work\Projects\python\dir2 E:\Work\Projects\python\test1.py '''
(2)遞歸獲取指定目錄下的目錄和文件列表(包括子目錄)
import os
path = os.getcwd()
#walk產(chǎn)生3元組:目錄路徑,目錄名稱,文件名
for dirpath,dirnames,filenames in os.walk(path):
print("dirpath:{};dirnames:{};filenames:{}".format(dirpath,dirnames,filenames))
'''運行結果:
dirpath:E:\Work\Projects\python;dirnames:['dir1', 'dir2'];filenames:['1.txt', '2.txt', 'test1.py']
dirpath:E:\Work\Projects\python\dir1;dirnames:[];filenames:['dir1_1.txt', 'dir1_2.txt']
dirpath:E:\Work\Projects\python\dir2;dirnames:[];filenames:[]
'''
for dirpath,dirnames,filenames in os.walk(path):
#輸出所有文件
for filename in filenames:
print(os.path.join(dirpath, filename))
#輸出所有目錄
for dirname in dirnames:
print(os.path.join(dirpath, dirname))
'''運行結果:
E:\Work\Projects\python\1.txt
E:\Work\Projects\python\2.txt
E:\Work\Projects\python\test1.py
E:\Work\Projects\python\dir1
E:\Work\Projects\python\dir2
E:\Work\Projects\python\dir1\dir1_1.txt
E:\Work\Projects\python\dir1\dir1_2.txt
'''
3、獲取文件信息
import os
import time
filepath = r'E:\Work\Projects\python\1.txt'
#分割路徑,返回一個元組(目錄,文件名),結果:('E:\\Work\\Projects\\python', '1.txt')
print(os.path.split(filepath))
#返回目錄部分,結果:E:\Work\Projects\python
print(os.path.dirname(filepath))
#返回文件名,結果:1.txt
print(os.path.basename(filepath))
#返回文件大?。▎挝粸樽止?jié)),結果:1296
print(os.path.getsize(filepath))
#返回目錄或文件的創(chuàng)建時間、最后修改時間、最后訪問時間(單位為新紀元1970年1月1日到訪問時的秒數(shù))
ctime = os.path.getctime(filepath)
mtime = os.path.getmtime(filepath)
atime = os.path.getatime(filepath)
#結果例子:1566436201.5443518 1566439077.5319004 1566439099.905073
print(ctime, mtime, atime)
#Unix時間戳轉換成時間
def unix2time(unix):
time_local = time.localtime(unix)
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
return dt
#將Unix時間戳轉換成時間
#結果例子:2019-08-22 09:10:01 2019-08-22 09:57:57 2019-08-22 09:58:19
print(unix2time(ctime),unix2time(mtime),unix2time(atime))
4、目錄和文件的移動、拷貝、刪除
當前目錄結構為
│ 1.txt │ 2.txt │ test1.py │ ├─dir1 │ dir1_1.txt │ dir1_2.txt │ └─dir2
import shutil
#拷貝整個目錄
shutil.copytree('dir1','dir1_bak')
#拷貝單個文件,第2個參數(shù)可以是目錄,也可以是文件名
shutil.copy('1.txt', 'dir2/1_bak.txt')
shutil.copy('2.txt', 'dir2')
#移動目錄或文件
shutil.move('dir1', 'dir2')
shutil.move('1.txt', 'dir2')
#刪除整個目錄
shutil.rmtree('dir1_bak')
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python列表reverse()函數(shù)使用方法詳解
這篇文章主要詳細介紹了Python列表reverse()函數(shù)使用方法,文章通過代碼示例講解的非常詳細,對我們的學習或工作有一定的幫助,需要的朋友可以參考下2023-07-07
火遍網(wǎng)絡的python中秋節(jié)賀卡現(xiàn)在學還趕得上
中秋將至,我用python編寫了個火遍網(wǎng)絡的中秋節(jié)賀卡,現(xiàn)在學起來還不晚,文中給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
python學生信息管理系統(tǒng)實現(xiàn)代碼
這篇文章主要介紹了python學生信息管理系統(tǒng)的實現(xiàn)代碼,代碼簡單,復制即可使用,需要的朋友可以參考下2019-12-12
Python matplotlib畫圖時圖例說明(legend)放到圖像外側詳解
這篇文章主要介紹了Python matplotlib畫圖時圖例說明(legend)放到圖像外側詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python logging 日志輪轉文件不刪除問題的解決方法
最近在維護項目的python項目代碼,項目使用了 python 的日志模塊 logging, 設定了保存的日志數(shù)目, 不過沒有生效,還要通過contab定時清理數(shù)據(jù)2016-08-08

