Python檔案練習(一)

2021-09-14 02:20:52 字數 630 閱讀 4513

import os

dir="f:\\mydir001"

#f=os.mkdir(dir)#建立資料夾

s="hello everyone"

l="\n改換行啦"

#寫入檔案,使用追加寫方式,此處注意資料夾連線方式

with open(dir+"\\002.txt","a") as file:

file.write(s)

file.write(l)

file.close()

#讀入檔案

file2=open(dir+"\\002.txt","r")

print(file2.read(5))

print(file2.read())

#下面兩個讀不到資料,已經被前面讀完

print(file2.readline())

print(file2.readlines())

file2.close()

hello

everyonehello everyone

改換行啦hello everyone

改換行啦hello everyone

改換行啦

python 檔案讀寫練習

練習包括 讀取與寫入 usr bin env python coding utf 8 time 2017 10 30 0030 08 58 file lianxi2.py 正向排序 import codecs l 1,5,3,8,2,4 l.sort 寫入檔案 with codecs.open 1....

python檔案練習(二)

import os 作業系統模組 print os.name print os.getcwd os.mkdir f x 建立目錄 os.makedirs f y z 建立多級目錄 print os.listdir f tom 將目錄,檔案返回為列表 os.rmdir f x 刪除目錄 os.remo...

python練習 讀寫檔案

對於不同的作業系統,檔案目錄會有不同,對於linux而言,萬事萬物皆檔案 對windows而言,檔案讀寫也很重要。import os 拼接路徑 os.path.join usr bin spam 返回路徑 usr bin spam 兩個倒斜槓等價於乙個正斜槓 獲得當前工作目錄 os.getcmd 相...