python 复制/移动文件
2023-09-27 14:27:48 时间
将某代码文件复制/移动到指定路径下,
例如:move ./xxx/git/project1/test.sh -> ./xxx/tmp/tmp/1/test.sh
相对路径./xxx/tmp/tmp/1/不一定存在
# -*- coding: utf-8 -*-
#!/usr/bin/python
#test_copyfile.py
import os,shutil
def mymovefile(srcfile,dstfile):
if not os.path.isfile(srcfile):
print "%s not exist!"%(srcfile)
else:
fpath,fname=os.path.split(dstfile) #分离文件名和路径
if not os.path.exists(fpath):
os.makedirs(fpath) #创建路径
shutil.move(srcfile,dstfile) #移动文件
print "move %s -> %s"%( srcfile,dstfile)
def mycopyfile(srcfile,dstfile):
if not os.path.isfile(srcfile):
print "%s not exist!"%(srcfile)
else:
fpath,fname=os.path.split(dstfile) #分离文件名和路径
if not os.path.exists(fpath):
os.makedirs(fpath) #创建路径
shutil.copyfile(srcfile,dstfile) #复制文件
print "copy %s -> %s"%( srcfile,dstfile)
srcfile='/Users/xxx/git/project1/test.sh'
dstfile='/Users/xxx/tmp/tmp/1/test.sh'
mymovefile(srcfile,dstfile)
相关文章
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
- Python-文件操作
- 利用Python做绝地科学家(外挂篇)
- python 的几种启动方式
- python中写入txt文件需要换行,以及r 和n
- Python绘制云图
- 【python】NLTK好文
- Python 读取WAV文件并绘制波形图
- Python DB API 连接数据库
- Python在mysql中进行操作是十分容易和简洁的
- (Python入门篇——3)数据类型初识
- 蓝桥杯青少组(Python组)考核知识范围
- Gerber file Gerber文件解析 .gbx光绘文件读取绘制 C++ Python OpenCV
- Python list 操作小记
- 免密登录-python
- Python 操作文件
- python Parent.__init()和super(Child, self)的区别
- python核心编程学习记录之文件和输入输出
- python报错:UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe0 in position 0: ordinal not in rang
- python小练习16 -- 多进程分割和合并一个文件
- PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象dockNestingEnabled属性
- Python文件学习遇到的问题
- 第5.2节 Python中带星号的函数参数实现参数收集
- python (3):wxPython打包app,报错
- python操作Redis之连接池
- Python numpy.correlate函数方法的使用
- python实现删除重复文件
- VS2019 C++ 调用python函数/类对象的方法
- python打印日志到控制台并将日志写入文件
- python学习之Flask框架:四个Session理解