import os path = os.getcwd() file_list = os.listdir(path) #列出子目录、子文件夹 for filename in file_list: if filename.endswith("py"): print(filename,end="\t") print("##########################################") file_list2 = [filename for filename in os.listdir(path) if filename.endswith("py")] for f in file_list2: print(f,end="\t") def factorial(n): if n ==1: return n else: return n*factorial(n-1) print(factorial(5))
import os os.startfile(r"D:\Program Files (x86)\WeChat\WeChat.exe")
with open("e","r",encoding="utf-8") as f: lines = f.readlines() lines = [line.rstrip()+" #"+str(index+1)+"\n" for index,line in enumerate(lines)] with open("e","w",encoding="utf-8") as f: f.writelines(lines) with open("aa.png","rb") as f: with open("aa_copy.png","wb") as w: for line in f.readlines(): w.write(line) print("图谱拷贝完成.........") with open("e","r",encoding = "utf-8") as f: print("文件名是:{0}".format(f.name)) print(f.tell()) print("读取内容:{0}".format(str(f.readline()))) print(f.tell) import pickle a1 = "张三" a2 = 123 a3 = [10,20,30] with open("data.dat","wb") as f: pickle.dump(a1,f) pickle.dump(a2,f) pickle.dump(a3,f) with open("data.dat","rb") as f: b1 = pickle.load(f) b2 = pickle.load(f) b3 = pickle.load(f) print(b1) print(b2) print(b3)
try: f = open(r"D:\test01.txt","w") strs = ["aa","bb","cc"] f.writelines(strs) finally: f.close() s = ["张三\n","李四\n","王五\n"] with open(r"D:\test01.txt","w") as f: f.writelines(s) with open(r"e","r",encoding="utf-8") as f: str = f.read() print(str)