標籤:
[1.os]
1.重新命名:os.rename(old, new)
2.刪除:os.remove(file)
3.列出目錄下的檔案 :os.listdir(path)
4.擷取當前工作目錄:os.getcwd()
5.改變工作目錄:os.chdir(newdir)
6.建立多級目錄:os.makedirs(r"c:/python /test")
7.建立單個目錄:os.mkdir("test")
8.刪除多個目錄:os.removedirs(r"c:/python") #刪除所給路徑最後一個目錄下所有空目錄。
9.刪除單個目錄:os.rmdir("test")
10.擷取檔案屬性:os.stat(file)
11.修改檔案許可權與時間戳記:os.chmod(file)
12.執行操作系統 命令:os.system("dir")
13.啟動新進程:os.exec(), os.execvp()
14.在後台執行程式:osspawnv()
15.終止當前進程:os.exit(), os._exit()
16.分離檔案名稱:os.path.split(r"c:/python/hello.py") --> ("c://python", "hello.py")
17.分離副檔名:os.path.splitext(r"c:/python/hello.py") --> ("c://python//hello", ".py")
18.擷取路徑名:os.path.dirname(r"c:/python/hello.py") --> "c://python"
19.擷取檔案名稱:os.path.basename(r"r:/python/hello.py") --> "hello.py"
20.判斷檔案是否存在:os.path.exists(r"c:/python/hello.py") --> True
21.判斷是否是絕對路徑:os.path.isabs(r"./python/") --> False
22.判斷是否是目錄:os.path.isdir(r"c:/python") --> True
23.判斷是否是檔案:os.path.isfile(r"c:/python/hello.py") --> True
24.判斷是否是連結檔案:os.path.islink(r"c:/python/hello.py") --> False
25.擷取檔案大小:os.path.getsize(filename)
26.*******:os.ismount("c://") --> True
27.搜尋目錄下的所有檔案:os.path.walk()
[2.shutil]
1.複製單個檔案:shultil.copy(oldfile, newfle)
2.複製整個分類樹:shultil.copytree(r"./setup", r"./backup")
3.刪除整個分類樹:shultil.rmtree(r"./backup")
[3.tempfile]
1.建立一個唯一的臨時檔案:tempfile.mktemp() --> filename
2.開啟臨時檔案:tempfile.TemporaryFile()
[4.StringIO] #cStringIO是StringIO模組的快速實現模組
1.建立記憶體 檔案並寫入初始資料 :f = StringIO.StringIO("Hello world!")
2.讀入記憶體檔案資料:print f.read() #或print f.getvalue() --> Hello world!
3.想記憶體檔案寫入資料:f.write("Good day!")
4.關閉記憶體檔案:f.close()
python之目錄檔案操作