# f=open('/Users/machou/Desktop/pyfils/hello2.txt','r', encoding="gb2312")
#每次讀1行
# for line in f.readlines():
# print(line.strip()) # 把末尾的'\n'刪掉
# f.close()
fpath=r'/Users/machou/Desktop/pyfils/hello2.txt'
#追加到文件末尾怎么辦器贩?可以傳入'a'以追加(append)模式寫入。
# with open('/Users/machou/Desktop/pyfils/hello2.txt','a',encoding='utf-8') as f:
# f.write('-啊-')
#不用考慮報錯,不用close()
# with open('/Users/machou/Desktop/pyfils/hello2.txt','r') as f:
# print(f.read())
# 查看當前目錄的絕對路徑:
os.path.abspath('.')
print(os.path.abspath('.'))
#拼接路徑
newdir= os.path.join(os.path.abspath('.'),'newDir')
print(newdir)
##創(chuàng)建文件夾,若已存在報錯
# os.mkdir(newdir)
##刪除文件夾
# os.rmdir(newdir)
os.path.split('/Users/michael/testdir/file.txt')
#('/Users/michael/testdir', 'file.txt')
os.path.splitext('/path/to/file.txt')
#('/path/to/file', '.txt')
for line in os.path.split('/Users/michael/testdir/file.txt/file222.txt'):
print(line) #/Users/michael/testdir/file.txt 和 file222.txt
for line in os.path.splitext('/Users/michael/testdir/file.txt/file22.txt'):
print(line) #/Users/michael/testdir/file.txt/file22 和 .txt
#當前文件夾新建文件,寫入內(nèi)容,重命名郑什,并刪除
currentDir= os.path.join(os.path.abspath('.'),'newfile.txt')
print(currentDir)
f=open(currentDir,'w') #會覆蓋原內(nèi)容
f.write('test new file')
f.close()
#對文件重命名:
os.rename('newfile.txt','rename-newfile.txt')
os.remove('rename-newfile.txt')
參考:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431925324119bac1bc7979664b4fa9843c0e5fcdcf1e000
# python獲取當前目錄下及子目錄下的所有文件名 http://blog.csdn.net/guoqianqian5812/article/details/52785746
import os
def getListFiles(path):
ret = []
for root, dirs, files in os.walk(path):
for filespath in files:
ret.append(os.path.join(root,filespath))
return ret
def getcount(num):
ret = num*num
return ret
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
print('-------------50--------------')
print(root) #當前目錄路徑
print('-------------51--------------')
print(dirs) #當前路徑下所有子目錄
print('-------------52--------------')
print(files) #當前路徑下所有非目錄子文件
#定義一個方法,復(fù)制一個文件夾下的所有文件,新文件名后+copy
def copyfiles(file_dir):
for root,dirs,files in os.walk(file_dir):
for afile in files:
if afile == '.DS_Store':
continue
newfilename=os.path.splitext(afile)[0]+'_copy'+os.path.splitext(afile)[1]
print(newfilename)
oldDirfilename = os.path.join(file_dir, afile)
newDirfileName = os.path.join(file_dir, newfilename)
with open(oldDirfilename,'r',encoding='utf-8') as f:
content = f.read()
# print(f.read())
with open(newDirfileName,'w',encoding='utf-8') as fson:
fson.write(content)
fson.close()
f.close()
print('-------------文件結(jié)束--------------')
if __name__ == '__main__':
ret = getListFiles(os.path.abspath('.'))
for each in ret:
print(each)
# for each in ret:
print(getcount(3))
print('-------------5--------------')
# file_name(os.path.abspath('.'))
print('-------------6--------------')
copyfiles('/Users/machou/Desktop/pyfils')
python 文件操作
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門规个,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人挥萌,你說我怎么就攤上這事绰姻。” “怎么了引瀑?”我有些...
- 文/不壞的土叔 我叫張陵狂芋,是天一觀的道長。 經(jīng)常有香客問我憨栽,道長帜矾,這世上最難降的妖魔是什么翼虫? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮屡萤,結(jié)果婚禮上珍剑,老公的妹妹穿的比我還像新娘。我一直安慰自己死陆,他們只是感情好招拙,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著措译,像睡著了一般别凤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上领虹,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼肚菠!你這毒婦竟也來了舔箭?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布,位于F島的核電站女蜈,受9級特大地震影響持舆,放射性物質(zhì)發(fā)生泄漏色瘩。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一逸寓、第九天 我趴在偏房一處隱蔽的房頂上張望居兆。 院中可真熱鬧,春花似錦竹伸、人聲如沸泥栖。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽聊倔。三九已至,卻和暖如春生巡,著一層夾襖步出監(jiān)牢的瞬間耙蔑,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 文件操作概述 我們通常會通過手工去打開牲尺、文本、寫入幌蚊、讀取文件等操作谤碳,其實我們也可以使用python程序?qū)ξ募M行打...
- fp=open('C:\\Users\\Administrator\\Desktop\\python\\text....
- # 8.第八次 Python文件操作 # 一、創(chuàng)建txt文件溢豆,寫入文本如下: # name,sex # '攀攀',...