1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author:woshinidaye
4
5 import os,sys
6 '''
7 print(os.getcwd()) #获取当前工作目录
8 os.chdir('e:\\') #切换目录
9 os.chdir(r'e:\\工作资料')
10 print(os.curdir) #当前路径
11 print(os.pardir)
12 os.makedirs(r'e:\工作资料\python\a\b') #创建文件夹,递归创建
13 os.makedirs(r'e:\工作资料\python\a\c') #创建文件夹,递归创建
14 os.removedirs(r'e:\工作资料\python\a\c') #删除文件夹,如果系列父文件夹都是空的,那就会递归删除
15 os.mkdir(r'e:\工作资料\python\a\d') #创建文件夹,不能递归创建
16 os.rmdir(r'e:\工作资料\python\a\f') #删除文件夹,即使父文件夹是空,也不会被删除
17 os.remove() #删除文件
18 os.renames(r'e:\工作资料\python\a',r'e:\工作资料\python\aaaa')
19 print(os.stat(r'e:\工作资料\python\1014_dict.py')) #查看文件或文件夹信息
20 print(os.sep) #获取系统的文件夹路径分隔符
21 print(os.pathsep) #获取系统文件夹区分的分隔符
22 print(os.name) #输出系统平台,nt windows
23 print(os.system('dir')) #执行系统命令
24 print(os.system('ipconfig')) #os.system的值会直接打印到屏幕,所以没法赋值给某一个变量
25 print(os.popen('dir')) #一串地址对象
26 print(os.popen('ipconfig').read()) #读取地址对象信息
27 print(os.path.abspath(__file__)) #获取绝对路径
28 print(os.path.split(r'e:\工作资料\python\a')) #分割目录,可以不考虑文件是否存在
29 # os.path.dirname() #返回路径,可以不考虑文件是否存在
30 print(os.path.basename(r'e:\工作资料\python\a')) #获取最后一层,可以不考虑文件是否存在
31 '''
32 # print(os.path.exists(r'e:\工作资料\python\aaaa')) #判断路径是否存在
33
34 # a = sys.argv
35 # print(a) #命令行list参数,第一个元素是程序本身的路径
36
37 import shutil #高级的文件、文件夹、压缩包处理工具
38
39
40
41 #copyfileobj() #需要先打开文件,再进行拷贝
42 '''
43 shutil.copyfileobj('src','des',30) #从一个文件对象copy到另外一个文件对象,30表示对象长度
44 a = os.path.dirname(os.path.abspath(__file__))+'\Reptile\h3c.html'
45 f_src = open(a,'r+',encoding='utf-8')
46 f_dst = open('test','w',encoding='utf-8')
47 shutil.copyfileobj(f_src,f_dst,30)
48 print('done!!')
49 '''
50 #copyfileobj()源码
51 '''
52 def copyfileobj(fsrc, fdst, length=0):
53 """copy data from file-like object fsrc to file-like object fdst"""
54 # Localize variable access to minimize overhead.
55 if not length:
56 length = COPY_BUFSIZE
57 fsrc_read = fsrc.read
58 fdst_write = fdst.write
59 while True:
60 buf = fsrc_read(length)
61 if not buf:
62 break
63 fdst_write(buf)
64 '''
65
66
67
68 # shutil.copyfile(src,dst) #COPY文件直接用这个,简单,不用先打开文件
69 a = os.path.dirname(os.path.abspath(__file__))+'\Reptile\h3c.html'
70 # shutil.copyfile(a,'b')
71
72
73
74 # shutil.copy() #COPY一个文件,The destination may be a directory.
75 #这个感觉是复制一个文件到另外一个目录,保留文件名称和属性
76 # shutil.copy(r'e:\工作资料\python\Reptile\h3c.html',r'e:\工作资料\python')
77 # print(os.stat(r'e:\工作资料\python\Reptile\h3c.html')) #两个文件的st_mode是一致的
78 # print(os.stat(r'e:\工作资料\python\h3c.html')) #两个文件的st_mode是一致的
79 '''
80 if os.path.isdir(dst):
81 dst = os.path.join(dst, os.path.basename(src))
82 copyfile(src, dst, follow_symlinks=follow_symlinks)
83 copymode(src, dst, follow_symlinks=follow_symlinks)
84 return dst
85 '''
86
87 # shutil.copystat() #仅对文件的权限进行
88
89 '''
90 # shutil.move() #移动一个文件或者文件夹
91 shutil.move(r'e:\工作资料\python\h3c.html',r'e:\工作资料\python\h4c.html') #跟Linux类似,还可以用来改名字
92 # Recursively move a file or directory to another location. This is
93 # similar to the Unix "mv" command. Return the file or directory's
94 # destination.
95 '''
96
97
98 '''
99 shutil.copytree() #递归复制文件夹、Recursively copy a directory tree and return the destination directory
100 shutil.rmtree() #递归删除文件夹、# Recursively delete a directory tree
101 '''
102
103 #压缩操作
104 # shutil.make_archive()
105 # shutil.make_archive('test','zip',base_dir=r'e:/工作资料/python/test_dir')
106 # """Create an archive file (eg. zip or tar).
107 # 'base_name' is the name of the file to create, minus any format-specific
108 # extension; 'format' is the archive format: one of "zip", "tar", "gztar",
109 # "bztar", or "xztar". Or any other registered format.
110 #压缩还可以用zipfile
111 import zipfile
112 '''#往里面写w一个文件,就表示向test.zip这个压缩包中添加一个文件,一个一个压缩文件,这个好用
113 z = zipfile.ZipFile(r'e:\工作资料\python\test_dir\test.zip','w')
114 z.write(r'e:\工作资料\python\h4c.html')
115 z.write(r'e:\工作资料\python\log.txt')
116 z.close()
117
118 z = zipfile.ZipFile(r'e:\工作资料\python\test_dir\test.zip','r') #解压就是r
119 z.extract() #提取一个包
120 z.close()
121 '''
122 #同理,还可以去研究一下tarfile
123 import xml.etree.ElementTree
124
125
126 #pyyaml #yaml格式多用于配置文件
127 #pip install -i https://pypi.douban.com/simple ruamel.yaml 需要先安装
128 #pip install ruamel.yaml
129 from ruamel.yaml import YAML
130
131
132 #configparser #常用于修改配置文件
133 import configparser
134 config = configparser.ConfigParser() #生成一个configparser的对象
135 #[]里面的内容表示section
136 #紧接着section的K-V就是option
137
138 #往里面写
139 '''
140 a = {
141 'name':'woshinidaye',
142 'age':18,
143 'male':'male'
144 }
145 config['test'] = a
146 with open(r'e:\工作资料\python\test_dir\config.ini','w+',encoding='utf-8') as f :
147 config.write(f)
148 '''
149
150 #修改option值,可以用set(section,option,value),也直接直接用列表的操作修改
151 #config.set方法对文件对option进行修改和增加
152 '''
153 config.read(r'e:\工作资料\python\test_dir\config.ini', encoding="utf-8")
154 config.set('test','age','28') #修改
155 config.set('test','school','jialidun') #这也可以用于增加
156 with open(r'e:\工作资料\python\test_dir\config.ini','w+',encoding='utf-8') as f :
157 config.write(f)
158 '''
159
160 #操作列表的方式增加
161 # a=config.read(r'e:\工作资料\python\test_dir\config.ini', encoding="utf-8")
162 # print(type(a)) #config.read是一个list
163 # config['test2'] = {} #创建一个section list没有item,所以这里创建的时候不同用[],只有字典才能
164 # config['test2'] = {"job":'doctor'} #这样也可以增加
165 # config['test2']['user'] = 'youself'
166 # with open(r'e:\工作资料\python\test_dir\config.ini','w+',encoding='utf-8') as f:
167 # config.write(f)
168
169 #操作列表的方式修改
170 # config['test2']['user'] = 'youself_lllaaa'
171 # with open(r'e:\工作资料\python\test_dir\config.ini','w+',encoding='utf-8') as f:
172 # config.write(f)
173
174 #获取option的值,get(section,option),获取整个section的信息,item(section)
175 '''
176 config.read(r'e:\工作资料\python\test_dir\config.ini', encoding="utf-8")
177 print(config.items('test'),config.items('test')[1]) #由tuple作为元素组成的list
178 print(config.get('test','age'))
179 '''
180 print(config.read(r'e:\工作资料\python\test_dir\config.ini', encoding="utf-8"))
181 # print(config.sections())
182 # print(config.add_section('test4'))
183 # with open(r'e:\工作资料\python\test_dir\config.ini','w+',encoding='utf-8') as f:
184 # config.write(f)
185
186 print('======done========')