os 和 os.path 模塊例子

import os

for tmpdir in ('/tmp', r'c:\temp'):
if os.path.isdir(tmpdir):
break
else:
print 'no temp directory available'

tmpdir = ''
if tmpdir:
os.chdir(tmpdir)
cwd = os.getcwd()
print '*** current temporary directory'
print cwd
print '*** creating example directory...'
os.mkdir('example')
os.chdir('example')
cwd = os.getcwd()
print '*** new working directory:'
print cwd
print '*** original directory listing:'
print os.listdir(cwd)
print '*** creating test file...'
fobj = open('test', 'w')
fobj.write('foo\n')
fobj.write('bar\n')
fobj.close()
print '*** updated directory listing:'
print os.listdir(cwd)

print "*** renaming 'test' to 'filetest.txt'"
os.rename('test', 'filetest.txt')
print '*** updated directory listing:'
print os.listdir(cwd)

path = os.path.join(cwd, os.listdir (cwd)[0])
print '*** full file pathname'
print path
print '*** (pathname, basename) =='
print os.path.split(path)
print '*** (filename, extension) =='
print os.path.splitext(os.path.basename(path))

print '*** displaying file contents:'
fobj = open(path)
for eachLine in fobj:
print eachLine
fobj.close()

print '*** deleting test file'
os.remove(path)
print '*** updated directory listing:'
print os.listdir(cwd)
os.chdir(os.pardir)
print '*** deleting test directory'
os.rmdir('example')
print '*** DONE'

  • 在 DOS 窗口下執(zhí)行這個例子輸出如下:
    *** current temporary directory
    C:\Users\gaolihong\Desktop\exercise
    *** creating example directory...
    *** new working directory:
    C:\Users\gaolihong\Desktop\exercise\example
    *** original directory listing:
    []
    *** creating test file...
    *** updated directory listing:
    ['test']
    *** renaming 'test' to 'filetest.txt'
    *** updated directory listing:
    ['filetest.txt']
    *** full file pathname
    C:\Users\gaolihong\Desktop\exercise\example\filetest.txt
    *** (pathname, basename) ==
    ('C:\Users\gaolihong\Desktop\exercise\example', 'filetest.txt')
    *** (filename, extension) ==
    ('filetest', '.txt')
    *** displaying file contents:
    foo

bar

*** deleting test file
*** updated directory listing:
[]
*** deleting test directory
*** DONE
[Finished in 0.1s]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市篙耗,隨后出現(xiàn)的幾起案子锨苏,更是在濱河造成了極大的恐慌笋婿,老刑警劉巖锰什,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異读虏,居然都是意外死亡嚼鹉,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門憨奸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來革屠,“玉大人,你說我怎么就攤上這事排宰∷浦ィ” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵额各,是天一觀的道長国觉。 經(jīng)常有香客問我,道長虾啦,這世上最難降的妖魔是什么麻诀? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮傲醉,結(jié)果婚禮上蝇闭,老公的妹妹穿的比我還像新娘。我一直安慰自己硬毕,他們只是感情好呻引,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著吐咳,像睡著了一般逻悠。 火紅的嫁衣襯著肌膚如雪元践。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天童谒,我揣著相機(jī)與錄音单旁,去河邊找鬼。 笑死饥伊,一個胖子當(dāng)著我的面吹牛象浑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播琅豆,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼愉豺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了茫因?” 一聲冷哼從身側(cè)響起蚪拦,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎节腐,沒想到半個月后外盯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體摘盆,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡翼雀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了孩擂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狼渊。...
    茶點(diǎn)故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖类垦,靈堂內(nèi)的尸體忽然破棺而出狈邑,到底是詐尸還是另有隱情,我是刑警寧澤蚤认,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布米苹,位于F島的核電站,受9級特大地震影響砰琢,放射性物質(zhì)發(fā)生泄漏蘸嘶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一陪汽、第九天 我趴在偏房一處隱蔽的房頂上張望训唱。 院中可真熱鬧,春花似錦挚冤、人聲如沸况增。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽澳骤。三九已至歧强,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間为肮,已是汗流浹背誊锭。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留弥锄,地道東北人丧靡。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像籽暇,于是被迫代替她去往敵國和親温治。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容