建一个免费网站的流程,seddog站长之家,系统软件开发,专业建网站的学校文件的读写#xff1a;
with open(filename, a, encodingutf-8) as file:with #xff1a;后面不必写close文件 第二个参数#xff1a;‘a’ 追加#xff1b;‘w’ 写#xff1b;‘r’ 读 encoding ‘utf-8’ 编码格式#xff0c;中文的话一般写上
enter y
while ent…文件的读写
with open(filename, a, encodingutf-8) as file:with 后面不必写close文件 第二个参数‘a’ 追加‘w’ 写‘r’ 读 encoding ‘utf-8’ 编码格式中文的话一般写上
enter y
while enter y:name input(请输入你的名字)filename guest_record.txtif name ! :with open(filename, a, encodingutf-8) as file:file.write(name \n)print(hello, , name, !)conti ywhile conti y:reason input(你为什么喜欢python)with open(filename, a, encodingutf-8) as file:file.write(reason \n)conti input(继续输入原因吗y/n )enter input(继续访问吗y/n )file.readlines() 文件按行读取存在列表内 file.read() 整体读取
filename pi_digits.txt
with open(filename) as pi_file: #with帮助我们适时关闭文件lines pi_file.readlines() #把文件按行存储
pi_str
for line in lines:pi_str line.strip() #strip()行左右的空删除
print(pi_str[:7]...)
print(len(pi_str))
birthday input(输入你的生日yyyymmdd )
if birthday in pi_str:print(你的生日出现在pi中。)
else:print(你的生日不在pi中。)filename learning_python.txt
with open(filename) as file:方法1:整个文件一次读取# print(file.read())方法2分行读取# for line in file.readlines():# print(line.strip())方法3line1 file.readlines()for l in line1:print(l.replace(Python, C).strip())tryexceptelsetry代码块出错后执行except部分未出错执行else 错误处理可以使程序不至于崩溃还可以继续运行
print(input 2 numbers to divide, enter q to quit.)
while True:first input(\nfirst num: )if first q:breaksecond input(\nsecond num: )try:answer int(first) / int(second)except ZeroDivisionError:print(divide zero!!!)else:print(answer)breakfilename learning_python.txt
try:with open(filename) as f_obj:contents f_obj.read()
except FileNotFoundError:msg Sorry, the file filename does not exist.print(msg)# pass #一言不发跳过
else:words contents.split()print(the title , filename, has , str(len(words)), words.)while True:print(input 2 nums : )try:a int(input(first num: ))except ValueError:print(请输入数字)continuetry:b int(input(second num: ))except ValueError:print(请输入数字)continueprint(sum of two nums is , ab)json文件存储
json.dump(object, file)
json.load(file)import json
numbers [2, 3, 5, 7, 11, 13]
filename numbers.json
with open(filename,w) as file:json.dump(numbers,file)with open(filename) as file:numbers json.load(file)
print(numbers)def get_stored_username():filename username.jsontry:with open(filename) as file:username json.load(file)except FileNotFoundError:return Noneelse:return usernamedef get_new_username():username input(What is your name? )filename username.jsonwith open(filename, a) as file:json.dump(username, file)return usernamedef greet_user():username get_stored_username()if username:print(Welcome back, , username, !)else:get_new_username()print(Well remember you when you come back, , username, !)greet_user()import json
def get_num():try:global favor_numfavor_num int(input(输入你喜欢的数字))except ValueError:print(你输入的不是数字请重新输入)get_num()return favor_numdef store_num(num):filename user_favor_num.jsonwith open(filename, a) as file:json.dump(num, file)def getAndStore():store_num(get_num())def print_num():filename user_favor_num.jsontry:with open(filename) as file:num json.load(file)except FileNotFoundError:getAndStore()else:print(i know your favorite number! it is , num)print_num()