four = open('output.txt','w')
line1 = "This here's the wa2ttle,\n"
four.write(line1)
# four.close() 用完要记得close
x = 52
four.write(str(x))
camels = 42
print('%d' % camels)
import os
cwd = os.getcwd() # current working directory
print(cwd)
""""绝对路径"""
print(os.path.abspath('memo.txt'))
"""检查路径是否存在"""
print(os.path.exists('memo.txt'))
"""检查对象是不是一个目录"""
print(os.path.isdir('memo.txt'))
print(os.path.isdir('D:/'))
"""检查对象是不是一个文件"""
print(os.path.isfile('output.txt'))
"""返回目录内的文件列表"""
lstf = os.listdir(cwd)
print(lstf)
def walk(dirname):
"""递归遍历文件夹中的文件,即如果有子文件夹,继续遍历"""
for name in os.listdir(dirname):
path = os.path.join(dirname,name)
if os.path.isfile(path):
print(path)
else:
walk(path)
""""
os.path.join 接收一个目录和一个文件名做参数, 然后把它们拼接成一个完整的路径
"""
try:
fin = open('bad_file')
except:
print('Something went wrong.')
"""数据库"""
import dbm
db = dbm.open('captions','c')
db['cleese.png'] = 'Photo of John Cleese.'
print(db['cleese.png'])
"""新值替换旧值"""
db['cleese.png'] = 'Photo of John Cleese doing a silly walk'
for key in db:
print(key,db[key])
db.close()