2019/05/23:目录文件操作练习

#encoding=utf-8
"""
1.	查找某个目录下是否存在某个文件名
"""
#先到指路径下,把里面所有的文件打印出来,再判断要找的文件是不是在目录下
import os
def fileIsExcist(path,file):
    os.chdir(path)#到目标目录下
    dirsAndFiles=os.listdir(path)
    if file in dirsAndFiles:
        print("恭喜你%s存在"%file)
    else:
        print("sorry,你要找的文件%s不存在指定的路径%s"%(file,path))
print(fileIsExcist("e:\\murphy","test.txt"))


#encoding=utf-8
"""
2.用系统命令拷贝文件
"""
#os.system(comment)#当前进程中打开一个子shell(子进程)来执行系统命令
import os
def executCopy():
    command="E:\\murphy\\file"
    print(command)
    os.chdir(command)
    os.system("copy video.txt "+"e:\\murphy")
print(executCopy())

#encoding=utf-8
"""
3.	输入源文件所在路径和目标目录路径,然后实现文件拷贝功能
"""
import shutil
def copySrcToDst(src,dst):
    shutil.copy(src,dst)

src="e:\\murphy\\a0.txt"
dst="e:\\murphy\\file"
dst1="e:\\murphy\\video.txt"
print(copySrcToDst(src,dst))
print(copySrcToDst(src,dst1))

#encoding=utf-8
"""
4.	遍历某个目录下的所有图片,并在图片名称后面增加_xx
"""
import os
def changePictureName(path):
    os.chdir(path)
    for dir_fil in os.listdir(path):
        if os.path.splitext(dir_fil)[1]==".jpg":
            new_name=os.path.splitext(dir_fil)[0]+"_XX"+os.path.splitext(dir_fil)[1]
            os.rename(dir_fil,new_name)
print(changePictureName("E:\\murphy\\file"))




猜你喜欢

转载自blog.csdn.net/sinat_18722099/article/details/90473723
今日推荐