Python文件及目錄操作實(shí)例詳解
本文實(shí)例講述了Python文件及目錄操作的方法。分享給大家供大家參考。具體分析如下:
在python中對(duì)文件及目錄的操作一般涉及多os模塊,os.path模塊。具體函數(shù)以及使用方法在程序中說(shuō)明。
#!/usr/bin/env python
#-*- coding=UTF8 -*-
import os
import os.path as op
def change_dir():
'''
該函數(shù)顯示及改變前目錄
using chdir() to change current dir
getcwd() can show the current working directory
'''
directory="/tmp"
#使用getcwd()返回當(dāng)前目錄
print os.getcwd()
#chdir改變當(dāng)前目錄為:directory目錄
os.chdir(directory)
print os.getcwd()
def show_filesOfdir(whichDir):
'''
此函數(shù)只顯示目錄下的所有文件
using listdir() to shows all of the file execpt directory
join() function catenate 'whichDir' with listdir() returns values
isfile() check that file is a regular file
'''
#listdir() 函數(shù)顯示前目錄的內(nèi)容
for file in os.listdir(whichDir):
#利用join()把whichDir目錄及l(fā)istdir() 返回值連接起來(lái)組成合法路徑
file_name = op.join(whichDir,file)
#isfile()函數(shù)可以判斷該路徑上的文件是否為一個(gè)普通文件
if op.isfile(file_name):
print file_name
def printaccess(path):
'''
顯示文件的最后訪問(wèn)時(shí)間,修改時(shí)間
shows 'path' the last access time
getatime() return the time of last access of path
stat() return information of a file,use its st_atime return the time of last access
ctime() return a string of local time
'''
import time
#利用ctime()函數(shù)返回最后訪問(wèn)時(shí)間
#getatime()函數(shù)返回最后訪問(wèn)時(shí)間,不過(guò)是以秒為單位(從新紀(jì)元起計(jì)算)
print time.ctime(op.getatime(path))
#stat()函數(shù)返回一個(gè)對(duì)象包含文件的信息
stat = os.stat(path)
#st_atime 最后一次訪問(wèn)的時(shí)間
print time.ctime(stat.st_atime)
print the modify time
print "modify time is:",
print time.ctime(op.getctime(path))
print "modify time is:",
#st_ctime 最后一次修改的時(shí)間
print time.ctime(stat.st_ctime)
def isDIR(path):
'''
一個(gè)os.path.isdir()函數(shù)的實(shí)現(xiàn)
Implement isdir() function by myself
'''
import stat
MODE = os.stat(path).st_mode
#返回真假值
return stat.S_ISDIR(MODE)
if __name__== "__main__":
change_dir()
show_filesOfdir('''/root''')
printaccess('/etc/passwd')
print isDIR('/etc')
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- python實(shí)現(xiàn)刪除文件與目錄的方法
- python 文件與目錄操作
- 詳解使用Python處理文件目錄的相關(guān)方法
- Python實(shí)現(xiàn)將目錄中TXT合并成一個(gè)大TXT文件的方法
- python檢測(cè)是文件還是目錄的方法
- python列出目錄下指定文件與子目錄的方法
- python實(shí)現(xiàn)支持目錄FTP上傳下載文件的方法
- python獲取目錄下所有文件的方法
- Python簡(jiǎn)單刪除目錄下文件以及文件夾的方法
- python獲取指定目錄下所有文件名列表的方法
- python文件操作之目錄遍歷實(shí)例分析
- python文件與目錄操作實(shí)例詳解
相關(guān)文章
python中is與雙等于號(hào)“==”的區(qū)別示例詳解
Python中有很多運(yùn)算符,下面這篇文章主要給大家介紹了關(guān)于python中is與雙等于號(hào)“==”區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
pandas讀取excel,txt,csv,pkl文件等命令的操作
這篇文章主要介紹了pandas讀取excel,txt,csv,pkl文件等命令的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
python使用minimize()?函數(shù)替代matlab的fmincon函數(shù)
這篇文章主要介紹了python使用minimize()函數(shù)替代matlab的fmincon函數(shù),在matlab中,fmincon函數(shù)可以用于求解帶約束的非線性多變量函數(shù)的最小值,即可以用來(lái)求解非線性規(guī)劃問(wèn)題2022-09-09
Django 重寫(xiě)用戶模型的實(shí)現(xiàn)
這篇文章主要介紹了Django 重寫(xiě)用戶模型的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Tensorflow設(shè)置顯存自適應(yīng),顯存比例的操作
今天小編就為大家分享一篇Tensorflow設(shè)置顯存自適應(yīng),顯存比例的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python嵌套式數(shù)據(jù)結(jié)構(gòu)實(shí)例淺析
這篇文章主要介紹了Python嵌套式數(shù)據(jù)結(jié)構(gòu),結(jié)合實(shí)例形式簡(jiǎn)單分析了Python字典與列表元素的嵌套存儲(chǔ)相關(guān)定義與操作技巧,需要的朋友可以參考下2019-03-03

