檔案學習內容

2021-09-12 13:22:11 字數 2863 閱讀 5912

a=

b=lambda x:x[『age』:12]

print(b(a))

a=,『002』:,

『003』:

}def hanshu():

number=input(「請輸入要插入的學號」)

if number in a.keys():#判斷學號是否在字典裡

print(「學號重複」)

else:

zd=#定義乙個空字典

zd[「name」]=input(「請輸入姓名」)#給zd字典裡新增姓名屬性

zd[「age」]=int(input(「請輸入年齡」))

zd[「address」]=input(「請輸入位址」)

a[number]=zd#把zd字典放到a裡面

hanshu()

print(a)

f=open(「a.txt」,『w』)#只以寫的方式開啟乙個檔案,叫a.txt,路徑在f盤下(絕對路徑) 若不寫路徑則儲存在本地目錄下(相對路徑)

f.write(「******x我愛你」)

f.close()

f=open(「a.txt」,「r」)

content=f.read()

print(content)

f.close()

f=open(「a.txt」,『r』)

content=f.readline()

print(content)

content=f.readline()

print(content)

f=open(「a.txt」,『r』)

content=f.readlines()#把所有行以列表的格式讀出

print(content)

f=open(『a.txt』,『w』)

f.write(「1234\n」)

#f.write("\n") 這兩個都可以換行

f.write(「45656」)

f.close()

f=open(『a.txt』,『a』)

f.write(「000000000」)

f.close()

f=open(『a.txt』,『r』)

x=f.read(4)

print(x)

print(f.tell())

f.close

f=open(『a.txt』,『r』)

x=f.read(4)

print(x)

f.seek(2,0)#第乙個引數代表偏移的位元組數,第二個引數代表從哪個地方開始偏移,0代表檔案頭,1代表當前,2代表末尾

x=f.read(7)

print(x)

f.close

import os #os 是作業系統

os.rename(『c.txt』,『d.txt』)

import os

os.remove(『d.txt』) #刪除

import os

os.mkdir(『acc』,0x777)

import os

p=os.getcwd()

print§

import os

p=os.listdir()

print§

import os

os.remove(『acc』)

import os

b=os.path.exists(「a.txt」)

print(b)

import os

b=os.path.exists("")

print(b)

import os

os.makedirs(「a//b//c」)#建立多級目錄

a=3b=a

a=4print(b)

import copy

a=[1,2,3,4]

b=iter(a)

print(type(b))

print(next(b))

print(next(b))

print(next(b))

print(next(b))

for x in a:

print(x)

def hanshu(n):

i=0while idef fbnq(n):

a,b=0,1

i=0while idef a(b):

b()def x():

print(「24343」)

a(x)

import time

def hx(f):#裝飾器的函式,f接受被裝飾的函式名

def neibu():#裝飾內部函式

start=time.time()

f()#呼叫被裝飾的函式

end=time.time()

print(end - start)

return neibu#裝飾器返回內部函式。(內部代表的是包裝盒)

@hx#@加函式名,代表下面的函式被hx裝飾

def jisuan():

print(「325243324」)

jisuan()

import time

def decor(funs):

def nb(cs):

print(「")

funs(cs)

print("

」)return nb

def dd(funs):

def nb(cs):

print("###########")

funs(cs)

print("###########")

return nb

@dd@decor

def ych(name):

print(name,「開演唱會」)

ych(「劉德華」)

學習筆記4 檢視檔案內容

檢視檔案內容可以通過cat tac head more less tail nl等指令 1 cat 一次性檢視檔案所有內容 cat 檔名 cat n 檔名 給每行加上行號,包括空行 cat b 檔名 給每行加上行號,不包括空行 跟cat正好相反 一次性檢視一屏內容 more 檔名 顯示開始的部分內容...

Python學習(8) 替換檔案內容

在前處理時替換檔案裡不被翻譯工具識別的內容。用 python 寫完指令碼後,實現 perl 指令碼呼叫 python 指令碼並執行。delete content 2.7.py coding utf 8 import os import re import io import sys str1 pri...

Linux學習之檔案內容檢視命令

linux系統中使用以下命令來檢視檔案的內容 cat 由第一行開始顯示檔案內容 語法 cat abentv 選項與引數 測試 檢視網路配置 檔案位址 etc sysconfig network scripts root lxw cat etc sysconfig network scripts if...