Python學習 檔案讀取

2021-09-25 15:19:25 字數 3454 閱讀 5122

# -*- coding: utf-8 -*-

"""created on sat jul 27 16:00:57 2019

@author: cc

"""#檔案操作

'''開啟當前目錄下檔案'''

#with會在不需要訪問檔案後將其關閉,避免使用close()關閉

#rstrip()函式用於刪除多餘的換行符

with

open

('digits.txt'

)as file_object:

contents=file_object.read(

)print

(contents.rstrip())

print

('\n'

)'''使用相對路徑訪問檔案'''

#.\表示上級目錄

with

open

('.\digits.txt'

)as file_object:

contents=file_object.read(

)print

(contents.rstrip())

print

('\n'

)'''使用絕對路徑讀取檔案'''

file_path=

'g:\python學習\digits.txt'

with

open

(file_path)

as file_object:

contents=file_object.read(

)print

(contents.rstrip())

print

('\n'

)'''逐行讀取'''

filename=

'digits.txt'

with

open

(filename)

as file_object:

for line in file_object:

print

(line.rstrip())

print

('\n'

)'''建立包含檔案內容的列表'''

#readlines()建立列表

#strip()刪除空格

filename=

'digits.txt'

digits=

with

open

(filename)

as file_object:

digits=file_object.readlines(

)pi_string=

''for line in digits:

#pi_string += line.rstrip()

pi_string += line.strip(

)print

(pi_string)

print

(float

(pi_string)

)print

(len

(pi_string)

)'''exercise'''

#全部讀取

filename=

'learning_python.txt'

with

open

(filename)

as file_object:

contents=file_object.read(

)print

(contents)

print

('\n'

)#按行讀取

with

open

(filename)

as file_object:

for line in file_object:

print

(line.rstrip())

print

('\n'

)#讀取成列表

with

open

(filename)

as file_object:

lines=file_object.readlines(

)'''拼接成一行字元'''

lines_string=

''for line in lines:

lines_string+=line.strip(

)print

(lines_string)

print

('\n'

)#replace()函式能夠臨時替換指定字元,但不會真正改變字串

print

(lines_string.replace(

'python'

,'c'))

print

('\n'

)print

(lines_string)

執行結果:

runfile('g:/python學習/file_operation.py', wdir='g:/python學習')

3.1414926535

8979323846

2643383279

3.1414926535

8979323846

2643383279

3.1414926535

8979323846

2643383279

3.1414926535

8979323846

2643383279

3.141492653589793238462643383279

3.1414926535897933

32in python you can learn many intresting things!

in python you can creative a magic world!

in python you can gain knowleges!

in python you can learn many intresting things!

in python you can creative a magic world!

in python you can gain knowleges!

in python you can learn many intresting things!in python you can creative a magic world!in python you can gain knowleges!

in c you can learn many intresting things!in c you can creative a magic world!in c you can gain knowleges!

in python you can learn many intresting things!in python you can creative a magic world!in python you can gain knowleges!

Spark學習 檔案讀取路徑)

在不同的啟動模式下,載入檔案時的路徑寫法是不一樣的,對於local模式下,預設就是讀取本地檔案,而在standlone或者yarn client,或者cluster模式下,預設讀的都是hdfs檔案系統,這幾種模式下很難讀取本地檔案 這是很顯然的事情,但你可以通過指定節點的檔案服務曲線救國 下面的 在...

Python學習 檔案操作

python使用open來開啟資料流 data open data.txt 下面是乙個讀取乙個檔案,然後逐行輸出的 try data open data.txt for each line in data try role,line spoken each line.split 1 print ro...

Python學習(檔案讀寫)

一 用os.makedirs 建立新資料夾 在桌面上建立乙個名稱為1的資料夾。import os os.makedirs c users king desktop 1 二 檢視檔案大小和資料夾內容 1.os.path.getsize path 返回path 引數中檔案的位元組數。import os ...