Python檔案與檔案系統

2021-07-13 17:17:15 字數 827 閱讀 9472

1.

import os

print("當前目錄是:",os.getcwd())

print(os.listdir())#這個返回的是當前目錄的檔案和目錄的名稱都是以列表形式返回的

2.__author__ = 'administrator'

#使用fileinput模組來處理乙個或者多個檔案的內容

import fileinput

def demo_fileinput():

with fileinput.input(["fpa.txt","fpb.txt"]) as lines:

for line in lines:

print("總行%d"%fileinput.lineno(),"目前讀取的檔案是%s,讀取到第%d行"%(fileinput.filename(),fileinput.filelineno()))

print(line.strip())

if __name__=="__main__":

demo_fileinput()

3.import os

#print("當前目錄是:",os.getcwd())

#print(os.listdir())#這個返回的是當前目錄的檔案和目錄的名稱都是以列表形式返回的

#os.mkdir("test02")

#print(os.listdir())

#os.rmdir("test02")

#print(os.listdir())

for k in os.walk(r"f:\baidu download"):#使用遞迴遍歷資料夾所有的資料夾和檔案

print(k)

檔案與檔案系統

1.檔案與檔案系統 open file,mode r buffering none,encoding none,errors none,newline none,closefd true open file and return a stream.raise oserror upon failure...

檔案與檔案系統

open file,mode r buffering none,encoding none,errors none,newline none,closefd true open file and return a stream.raise oserror upon failure.file 必需,檔...

檔案與檔案系統

1.檔案與檔案系統 open file,mode r buffering none,encoding none,errors none,newline none,closefd true open file and return a stream.raise oserror upon failure...