---python_os---python操作檔案的筆記

來源:互聯網
上載者:User

標籤:目錄   font   為什麼   修改檔案許可權   sdi   os.path   環境   etc   指定   

 1 #! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import os 4  5 __author__ = ‘以德服人‘ 6 # 擷取目前的目錄 7 currentpath = os.getcwd() 8 print(currentpath) 9 # C:\Users\以德服人\PycharmProjects\PyDemo10 11 # 返回指定目錄下的所有檔案和目錄名12 print(os.listdir(currentpath))13 # [‘.idea‘, ‘dict.py‘, ‘file.py‘, ‘fonin_02.py‘,14 # ‘forin_02_v1.py‘, ‘format_01.py‘, ‘list_01.py‘, ‘list_02.py‘, ‘test.py‘, ‘zfc.py‘]15 16 17 # 判斷一個路徑是否真實存在18 path = currentpath + "\ceshi.py"19 if os.path.exists(path):20     # 如果存在刪除21     # removedirs 刪除空目錄,如果有目錄有內容不能刪除22     os.remove(path)23 else:24     print("file is not exist")25 26 # 判斷給出的路徑是否是一個檔案,返回boolean27 print(os.path.isfile(currentpath+"/file.py"))28 # 判斷給出的路徑是否是一個目錄29 print(os.path.isdir(currentpath))30 # 判斷是否是絕對路徑31 print(os.path.isabs(currentpath))32 # 判斷是否是連結33 print(os.path.islink(‘http://www.baidu.com‘))34 # 為什麼返回false?http://www.baidu.com 這不是串連嗎35 36 37 38 # 返回一個路徑的目錄名和檔案名稱39 print(os.path.split(currentpath))40 # (‘C:\\Users\\以德服人\\PycharmProjects‘, ‘PyDemo‘)41 print(os.path.split(currentpath+"/file.py"))42 # (‘C:\\Users\\以德服人\\PycharmProjects\\PyDemo‘, ‘file.py‘)43 # (‘C:\\Users\\以德服人\\PycharmProjects\\PyDemo‘, ‘file.py‘)44 45 # 分離副檔名46 print(os.path.splitext(currentpath+"/file.py"))47 # (‘C:\\Users\\以德服人\\PycharmProjects\\PyDemo/file‘, ‘.py‘)48 49 # 擷取路徑名50 print(os.path.dirname(currentpath))51 # C:\Users\以德服人\PycharmProjects52 print(os.path.dirname(currentpath+"/file.py"))53 # C:\Users\以德服人\PycharmProjects\PyDemo54 55 # 擷取檔案名稱56 print(os.path.basename(currentpath))57 # PyDemo58 print(os.path.basename(currentpath+"/file.py"))59 # file.py60 61 # 擷取系統名 對於Windows,它是‘nt‘,而對於Linux/Unix使用者,它是‘posix‘  62 print(os.name)63 # nt64 65 # linux下命令66 # 讀取和設定環境變數:os.getenv() 與os.putenv() 67 # 給出當前平台使用的行終止符  Windows使用‘\r\n‘,Linux使用‘\n‘而Mac使用‘\r‘68 # windows和linux的路徑有點不一樣,windows是用 \\ 來分割的,linux是用 / 來分隔,  69 # os.sep 會自動根據系統選擇用哪個分隔字元。 70 71 # 重新命名72 # 先進入目錄73 os.chdir(currentpath)74 os.rename("dict.py", "dict1.py")75 os.renames("dict1.py", "dict2.py")
# 建立多級目錄:os.makedirs# os.makedirs(‘d:\h\i\z\k‘)# 建立單個目錄:os.mkdirprint(os.stat(currentpath+"/file.py"))# os.stat_result(st_mode=33206, st_ino=27866022694406647, st_dev=4238402480,# st_nlink=1, st_uid=0, st_gid=0, st_size=2740, st_atime=1481207487, st_mtime=1481207487, st_ctime=1481204713)# 修改檔案許可權與時間戳記:os.chmod(path,mode) 只需要2個參數,一個是路徑,一個是說明路徑的模式# 終止當前進程:os.exit() # 擷取檔案大小print(os.path.getsize(currentpath+"/file.py"))# 3102

  。。。 http://www.runoob.com/python/os-file-methods.html  這個網站更詳細。。。

---python_os---python操作檔案的筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.