一.python中文件操作
1.最基本的文件操作当然就是在文件中读写数据。这也是很容易掌握的。现在打开一个文件以进行写操作:
1. fileHandle = open ( 'test.txt', 'w' )
2.fileHandle = open ( 'test.txt', 'w' )
2.‘w’是指文件将被写入数据,语句的其它部分很好理解。下一步就是将数据写入文件:
1. fileHandle.write ( 'This is a test.\nReally, it is.' )
这个语句将“This is a test.”写入文件的第一行,“Really, it is.”写入文件的第二行。最后,我们需要做清理工作,并且关闭文件:
2. fileHandle.close()
3.你再次使用“w”方式在文件中写数据,所有原来的内容都会被删除。如果想保留原来的内容,可以使用“a”方式在文件中结尾附加数据:
1. fileHandle = open ( 'test.txt', 'a' )
2. fileHandle.write ( '\n\nBottom line.' )
3. fileHandle.close()
fileHandle = open ( 'test.txt', 'a' )
fileHandle.write ( '\n\nBottom line.' )
fileHandle.close()
练习展示
1.用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:
f=open('f.txt','w') # r只读,w可写,a追加
for i in range(0,10):f.write(str(i)+'\n')
. . .
f.close()
2.文件内容追加,从0到9的10个随机整数:
#python
import random
f=open('f.txt','a')
for i in range(0,10):f.write(str(random.randint(0,9)))
. . .
f.write('\n')
f.close()
3.文件内容追加,从0到9的随机整数, 10个数字一行,共10行:
>>> import random
>>> f=open('f.txt','a')
>>> for i in range(0,10):
. . . for i in range(0,10):f.write(str(random.randint(0,9)))
. . . f.write('\n')
. . .
>>> f.close()
二.python中文件路径操作
1.调用库
from pathlib import
2.创建path对象
p = Path('D:/python/1.py')
print(p)
#可以这么使用,相当于os.path.join()
p1 = Path('D:/python')
p2 = p1/'123'
print(p2)
3.获取当前文件的信息
p = Path('1.py')
print(p.stat())
4.判断当前路径是否是文件或者文件夹
Path('.').exists()
True
Path('1.py').exists()
True
Path('2.py').exists()
False
5.Path.glob(pattern):获取路径下的所有符合pattern的文件,返回一个generator
Path.glob(pattern)
Path.rglob(pattern)
path=Path.cwd()
pys = path.glob(’*.py’)#返回但钱以。py结尾的文件或者目录
Path.rglob(pattern):与上面类似,只不过是返回路径中所有子文件夹的符合pattern的文件
6.Path.is_dir()与Path.is_file()
Path.is_dir()判断该路径是否是文件夹
Path.is_file()判断该路径是否是文件
print('p1:')
p1 = Path('D:/python')
print(p1.is_dir())
print(p1.is_file())
print('p2:')
p2 = Path('D:/python/1.py')
print(p2.is_dir())
print(p2.is_file())
#当路径不存在时也会返回Fasle
print('wrong path:')
print(Path('D:/NoneExistsPath').is_dir())
print(Path('D:/NoneExistsPath').is_file())
7.Path.iterdir() 迭代当前目录下的文件
p = Path.cwd()
for i in p.iterdir():
print(i)
D:\python\1.py
D:\python\11.py
D:\python\1111.py
D:\python\11111.py
D:\python\dir
8.Path.parent(),Path.parents()
parent获取path的上级路径,parents获取path的所有上级路径
9.Path.is_absolute()
判断path是否是绝对路径
10…Path.rmdir()
当path为空文件夹的时候,删除该文件夹
每周算法-------堆排序
origin = [0, 50, 10, 90, 30, 70, 40, 80, 60, 20]
n = len(origin) - 1 # 表示一共有多少个元素
def heap_adjudt(x, z, arry):
# print(z)
# print(x)
while 2 * z < x:
lmaxindex = 2 * z
# print(lmaxindex)
if arry[lmaxindex] < arry[2 * z + 1]:
# print(arry[lmaxindex])
lmaxindex = 2 * z + 1
# print(lmaxindex)
else:
pass
if arry[lmaxindex] > arry[z]:
print(arry[lmaxindex])
arry[lmaxindex], arry[z] = arry[z], arry[lmaxindex]
z = lmaxindex
print(z)
else:
break
# print(arry)
#heap_adjudt(n,1,origin)
构建大顶堆
def max_heap(total, arry):
# print(arry)
for i in range(total // 2, 0, -1):
print(total)
heap_adjudt(total, i, arry)
print(arry)
排序
def sort(total,arry):
while total>1:
arry[1],arry[total]=arry[total],arry[1]
total-=1
if total==2 and arry[total]>arry[1]:
break
heap_adjudt(total,1,arry)
print(arry)
感谢大家支持:
欢迎志同道合的朋友:成为小灰灰的朋友