本文主要是介绍Python3,代码磁贴,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import os#导入标准库“os”
print(os.getcwd())#当前的工作目录os.chdir("HeadFirstPython")#切换文件夹
os.chdir("chapter3")
os.getcwd()
man = []
other = []
try:
data = open("sketch.txt")
for each_line in data:
try:
(role,line_spoken) = each_line.split(":",1)
line_spoken = line_spoken.strip()
if role == "Man":
man.append(line_spoken)
elif role == "Other Man":
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print("The data file is missing!")
print(man)
print(other)
这篇关于Python3,代码磁贴的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!