# 1.路径操作
# 当前工作目录 os.getcwd() 函数获取当前工作路径的字符串
import os
path = os.getcwd()
print(path)
# os.chdir() 改变当前路径
os.chdir('/Users/wang/Desktop')
path = os.getcwd()
print(path)
# os.makedirs() 创建文件夹
os.makedirs('/Users/wang/Desktop/python')
# os.path() 模块
# 处理绝对路径和相对路径
# 1.调用os.path.abspath(path)将返回参数的绝对路径的字符串,这是将相对路径转换为绝对路径的方法.
# 2.调用os.path.isabs(path),如果参数是一个绝对路径,就返回True,如果参数是一个相对路径,返回Falses.
# 3.调用os.path.relpath(path,start)将返回从start路径到path的相对路径的字符串。如果没有提供start,则从当前目录作为开始路径
path =os.path.abspath('.')
print(path)
path_1 = os.path.relpath('/Users/wang/Desktop', '/Users')
print(path_1)
path_1 = os.getcwd()
print(path_1)
# os.path.dirname(path) 将返回一个字符串,它包含path参数中最后一个斜杠之前的内容。
# os.path.basename(path)将返回一个字符串,它包含path参数中最后一个斜杠之后的内容。
path = '/Users/wang/Desktop'
path_1 = os.path.dirname(path)
print(path_1)
path_1 = os.path.basename(path)
print(path_1)
# 如果需要两个值, 调用os.path.split()
path_1 = os.path.split(path)
print(path_1)
# 查看文件大小和文件内容
# os.path.getsize(path) 返回参数中文件的字节数
# os.listdir(path) 返回文件名字符串的列表,包含path参数中每个文件
nCnt = os.path.getsize('/Users/wang/Desktop/python/test_1.py')
print(nCnt)
name = os.listdir('/Users/wang/Desktop/python')
print(name)
# 查看文件中所以文件的总字数
nsize = 0
for name in os.listdir('/Users/wang/Desktop/python'):
nsize += os.path.getsize(os.path.join('/Users/wang/Desktop/python',name))
print(nsize)
# 检查路径的有效性
# 如果路径不存在,python会崩溃报错。
# os.path.exists(path) 判断文件或者文件夹是否存在,存在返回True.
# os.path.isfile(path) 判断是不是一个文件,是返回True.
# os.path.isdir(path) 判断是不是一个文件夹,是返回True.
# 2.读写文件过程
# open(path,mode) 函数,打开文件,返回File对象 mode默认读模式,'r'-只读 'w'-只写 从头覆盖 'a'-追加写入
# read() 读 函数 -- write() 写 函数
# close() 关闭文件
hfile = open('/Users/wang/Desktop/python/h.txt')
content = hfile.read()
print(content)
hfile.close()
hfile = open('/Users/wang/Desktop/python/h.txt','w')
Cnt = hfile.write('Kyle\nboy')
print(Cnt)
hfile.close()
# readlines()方法,从文件取得一个字符串的列表。列表中的每个字符串就是文本中的每一行。
hefile = open('/Users/wang/Desktop/python/h.txt')
content = hefile.readlines()
print(content)
hefile.close()
# 3.用shelve模块保存变量
# shelve模块可以将python程序中的变量保存到二进制的shelf文件中。
# shelve模块可以让你在程序中添加"保存"和"打开"功能
# 如果运行一个程序,并输入一些配置,就可以将这些设置保存到一个shelf文件,然后让程序下一次运行时加载它们。
import shelve
shelfFile = shelve.open('mydata')
cats = ['Zophie','Pooks','Simon']
shelfFile['cats'] = cats
shelfFile.close();
#结果生成 mydata.db 文件
# 重新打开并读取数据
shelfFile = shelve.open('mydata')
str_1 = type(shelfFile)
print(str_1)
#结果:<class 'shelve.DbfilenameShelf'>
str_1 = shelfFile['cats']
print(str_1)
#结果:['Zophie', 'Pooks', 'Simon']
shelfFile.close()
# shelf值有keys()和values方法,
shelfFile = shelve.open('mydata')
str_1 = list(shelfFile.keys())
print(str_1)
#结果:['cats']
str_1 = list(shelfFile.values())
print(str_1)
#结果:[['Zophie', 'Pooks', 'Simon']]
shelfFile.close()
# 用pprint.pformat()函数保存变量
import pprint
cats = [{'name':'kyle','age':'25'},{'name':'John', 'age':'26'}]
str_1 = pprint.pformat(cats)
print(str_1)
#结果:[{'age': '25', 'name': 'kyle'}, {'age': '26', 'name': 'John'}]
fileObj = open('myCats.py','w')
fileObj.write('cats = ' + pprint.pformat(cats) + '\n')
fileObj.close()
# 使用myCats.py
import myCats
print(myCats.cats)
#结果:[{'age': '25', 'name': 'kyle'}, {'age': '26', 'name': 'John'}]
str_1 = myCats.cats[0]
print(str_1)
#结果:{'age': '25', 'name': 'kyle'}
str_1 = myCats.cats[0]['name']
print(str_1)
#结果:kyle