python练习2020/10/24

12.用系统命令拷贝文件
import os
os.chdir(‘e:\test’)
os.system(‘copy 333.txt e:\’)

13.输入源文件所在路径和目标目录路径,然后实现文件拷贝功能

import shutil
def copy_file(source,dest):
shutil.copyfile(source,dest)

copy_file(‘e:\test\333.txt’,‘e:\33.txt’)

14.遍历某个目录下的所有图片,并在图片名称后面增加_xx
import os
os.chdir(‘e:\test’)
for root,dirs,files in os.walk(‘e:\test’):
for file in files:
if ‘.JPG’ in file:
new_name=os.path.splitext(os.path.join(root,file))[0]+’_xx’+os.path.splitext(os.path.join(root,file))[1]
os.rename(file,new_name)

#2

import os
def parse_pic(path):
os.chdir(path)
for dir_file in os.listdir(path):
if os.path.splitext(dir_file)[1]==’.JPG’:
new_name=os.path.splitext(dir_file)[0]+’_xx’+os.path.splitext(dir_file)[1]
os.rename(dir_file,new_name)

print(parse_pic(‘e:\test’))

15、遍历指定目录下的所有文件,找出其中占用空间最大的前3个文件

import os
result={}
os.chdir(‘e:\test’)
for root,dirs,files in os.walk(‘e:\test’):
for file in files:
print(file)
file_name=os.path.join(root,file)
result[file]=os.path.getsize(file_name)

print(sorted(result.items(),key=lambda x:x[-1],reverse=True)[:3])

#2

import os
os.chdir(‘e:\test’)
def get_size(path):
result={}
for dir_file in os.listdir(path):
if os.path.isfile(dir_file):
result[dir_file]=os.path.getsize(dir_file)
return dict(sorted(result.items(),key=lambda x:x[-1],reverse=True)[:3])
print(get_size(‘e:\test’))

16、过滤py源码中的#注释,另存为文件result.py,并执行result.py,断言是否执行成功
import os
import sys

with open(‘e:\test\b.py’)as fp:
for line in fp:
if not line.startswith(’#’):
with open(‘e:\test\result.py’,‘a’,encoding=‘utf-8’)as fp:
fp.write(line)

17、文件访问,提示输入数字 N 和文件 F, 然后显示文件 F 的前 N 行
import os
n=int(input(‘请输入数字n:’))
F=input(‘请输入文件:’)
with open(F,‘r’,encoding=‘utf-8’)as fp:
content=fp.readlines()
print(content[:n])

#2
import os
n=int(input(‘请输入数字n:’))
F=input(‘请输入文件:’)
with open(F,‘r’,encoding=‘utf-8’)as fp:
for i in range(n):
print(fp.readline())

18、从命令行接受1个路径如:c:\a\b\c\1.py,
 实现1个函数创建目录a\b\c,创建文件1.py,
实现1个函数删除已创建的目录及文件

file_path = input("请输入文件路径: ")
import os
def create(file_path):
    dirs = os.path.split(file_path)[0]
    file = os.path.split(file_path)[1]
    if not os.path.exists(dirs):
        os.makedirs(dirs)
    os.chdir(dirs)
    with open(file,"w") as file_obj:
        pass
print(create(file_path))

def remove(file_path):
    dirs = os.path.split(file_path)[0]
    file = os.path.split(file_path)[1]
    os.chdir(dirs)
    if os.path.exists(file):
        os.remove(file)
    os.removedirs(dirs)
        

print(remove(file_path))


20、实现DOS命令执行功能,接受输入命令并执行,然后把执行结果和返回码打印到屏幕

>>> command=input('请输入命令:')
请输入命令:dir
>>> os.system(command)

22、同时读写文件
import os
with open('e:\\test\\22.txt','r+',encoding='utf-8')as fp:
    for line in fp:
        with open('e:\\test\\33.txt','a',encoding='utf-8')as fp:
            fp.write(line)

#2
with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    content=fp.readlines()
    with open('e:\\test\\333.txt','w',encoding='utf-8')as fp:
        fp.writelines(content)

23、创建一个空文件
with open('e:\\test\\444.txt','w',encoding='utf-8')as fp:
    pass

24、读取文件的前两行

with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    content=fp.readlines()
    for i in content[:2]:
        print(i)

#2
with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    content=fp.readlines()
    print(content[:2])

25、读取文件的奇数行
with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    content=fp.readlines()
    for i in range(len(content)):
        if i%2 !=0:
            print(content[i])

26、在文件中写入一个列表的内容
with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    content=fp.readlines()
    with open('e:\\test\\55.txt','w',encoding='utf-8')as fp:
        fp.writelines(content)

with open('e:\\test\\55.txt','r',encoding='utf-8')as fp:
    print(fp.read())

27、在文件中的024位置写入当前的文件位置偏移量
with open('e:\\test\\22.txt','r+',encoding='utf-8')as fp:
    fp.seek(0,0)
    fp.write(str(fp.tell()))
    fp.seek(2,0)
    fp.write(str(fp.tell()))
    fp.seek(4,0)
    fp.write(str(fp.tell()))

28with写法读取文件内容
with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    print(fp.readlines())

29、统计一个文件中单词的个数
import string
with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    content=fp.read()
    for line in content:
        for word in line:
            #print(word)
            if not (word >='a' and word <='z') and not (word >='A' and word <='Z'):
                content=content.replace(word,' ')
word_count=0
for i in content.split():
    word_count+=1
print('文件中单词个数为:',word_count)

30、将一个文件的所有单词倒序写入文件中
with open('e:\\test\\22.txt','r',encoding='utf-8')as fp:
    content=fp.read()
    for line in content:
        for word in line:
            if not (word >='a' and word <='z') and not (word >='A' and word <='Z'):
                content=content.replace(word,' ')
print(content)
content=content.split() 
print(content)
with open('e:\\test\\99.txt','a',encoding='utf-8')as fp:
    for i in content[::-1]:
        fp.write(i+' ')



你可能感兴趣的:(python,1024程序员节)