python文件與目錄操作實(shí)例詳解
本文實(shí)例分析了python文件與目錄操作的方法。分享給大家供大家參考,具體如下:
關(guān)于python文件操作的詳細(xì)說明,大家可以參考前一篇《python文件操作相關(guān)知識點(diǎn)總結(jié)整理》
官方API:os-Miscellaneous operating system interfaces
下面是demo示例:
import re
import os
import time
#圖片文件路徑
image_path = 'E:\\test\\20130627_140132Hongten.jpg'
#文件夾路徑
dir_path = 'E:\\test\\hongten'
#文件路徑
file_abs_path = 'E:\\test\\hongten.txt'
#得到當(dāng)前工作空間目錄
def getcwd():
return os.getcwd()
#獲取指定文件夾下面的所有文件及文件夾
#如果指定的文件夾不存在,則返回相應(yīng)的提示信息
def listdir(dir_path):
if os.path.exists(dir_path):
return os.listdir(dir_path)
else:
return '目錄'+ dir_path + '不存在'
def isfile(file_path):
if os.path.exists(file_path):
return os.path.isfile(file_path)
else:
return '文件'+ dir_path + '不存在'
if __name__ == '__main__':
print('當(dāng)前的工作空間是:{0}'.format(getcwd()))
print('當(dāng)前的工作空間下的文件及目錄:',listdir(getcwd()))
print('#' * 40)
print(listdir('c:\\test'))
print('#' * 40)
print(isfile(image_path))
print('#' * 40)
array = os.path.split(image_path)
print(array)
#文件全名:20130627_140132Hongten.jpg
file_full_name = array[1]
name = os.path.splitext(file_full_name)
#文件名:20130627_140132Hongten
file_name = name[0]
#文件后綴:.jpg
file_ext = name[1]
print('文件全名:{0},文件名:{1},文件后綴:{2}'.format(file_full_name,file_name,file_ext))
print('#' * 40)
#創(chuàng)建空文件夾
#os.mkdir('E:\\mydir')
#創(chuàng)建多級目錄
#os.makedirs(r'E:\\bb\\cc')
print('#' * 40)
#打開一個(gè)文件
fp = open(file_abs_path,'w+')
#print('讀取文件:{0}的第一行:{1}'.format(file_abs_path,fp.readline()))
#把文件每一行作為一個(gè)list的一個(gè)成員,并返回這個(gè)list。其實(shí)它的內(nèi)部是通過循環(huán)調(diào)用readline()來實(shí)現(xiàn)的。
#如果提供size參數(shù),size是表示讀取內(nèi)容的總長,也就是說可能只讀到文件的一部分。
#print('讀取文件:{0}所有內(nèi)容:{1}'.format(file_abs_path,fp.readlines()))
content = 'this is a test message!!\ngood boy!\ngogo......\nhello,I\'m Hongten\nwelcome to my space!'
fp.write(content)
fp.flush()
fp.close()
fp = open(file_abs_path,'r+')
print('讀取文件:{0}所有內(nèi)容:{1}'.format(file_abs_path,fp.readlines()))
運(yùn)行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
當(dāng)前的工作空間是:D:\Python33\workspace
當(dāng)前的工作空間下的文件及目錄: ['rename.py', 'test_annotation.py', 'test_class.py', 'test_exception.py', 'test_exit.py', 'test_file.py', 'test_getA.py', 'test_hello.py', 'test_import.py', 'test_input.py', 'test_loops.py', 'test_myclass.py', 'test_os.py', 'test_range.py', 'test_str.py', 'test_string.py', 'test_while.py', 'test_with.py']
########################################
目錄c:\test不存在
########################################
True
########################################
('E:\\test', '20130627_140132Hongten.jpg')
文件全名:20130627_140132Hongten.jpg,文件名:20130627_140132Hongten,文件后綴:.jpg
########################################
########################################
讀取文件:E:\test\hongten.txt所有內(nèi)容:['this is a test message!!\n', 'good boy!\n', 'gogo......\n', "hello,I'm Hongten\n", 'welcome to my space!']
>>>
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python之inspect模塊實(shí)現(xiàn)獲取加載模塊路徑的方法
今天小編就為大家分享一篇Python之inspect模塊實(shí)現(xiàn)獲取加載模塊路徑的方法,具有很好的價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python+Socket實(shí)現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能示例
這篇文章主要介紹了Python+Socket實(shí)現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能,結(jié)合實(shí)例形式分析了Python+socket實(shí)現(xiàn)UDP協(xié)議廣播的客戶端與服務(wù)器端功能相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Jupyter Notebook 文件默認(rèn)目錄的查看以及更改步驟
這篇文章主要介紹了Jupyter Notebook 文件默認(rèn)目錄的查看以及更改步驟,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
深入理解Python虛擬機(jī)中描述器的實(shí)現(xiàn)原理
這篇文章主要給大家介紹一個(gè)我們在使用類的時(shí)候經(jīng)常使用但是卻很少在意的黑科技——描述器的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-05-05

