Windows下用Python你会几种copy文件的方法?
来源:互联网 发布:线路板用什么软件 编辑:程序博客网 时间:2024/04/30 10:20
1. os.system
2. shutil.copy和shutil.copytree
3. win32file.CopyFile
4. SHFileOperation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import
os
import
tempfile
filename1
=
tempfile.mktemp (
".txt"
)
open
(filename1,
"w"
).close ()
filename2
=
filename1
+
".copy"
print
filename1,
"=>"
, filename2
#拷文件
os.system (
"copy %s %s"
%
(filename1, filename2))
if
os.path.isfile (filename2):
print
"Success"
dirname1
=
tempfile.mktemp (
".dir"
)
os.mkdir (dirname1)
dirname2
=
dirname1
+
".copy"
print
dirname1,
"=>"
, dirname2
#拷目录
os.system (
"xcopy /s %s %s"
%
(dirname1, dirname2))
if
os.path.isdir (dirname2):
print
"Success"
2. shutil.copy和shutil.copytree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import
os
import
shutil
import
tempfile
filename1
=
tempfile.mktemp (
".txt"
)
open
(filename1,
"w"
).close ()
filename2
=
filename1
+
".copy"
print
filename1,
"=>"
, filename2
#拷文件
shutil.copy (filename1, filename2)
if
os.path.isfile (filename2):
print
"Success"
dirname1
=
tempfile.mktemp (
".dir"
)
os.mkdir (dirname1)
dirname2
=
dirname1
+
".copy"
print
dirname1,
"=>"
, dirname2
#拷目录
shutil.copytree (dirname1, dirname2)
if
os.path.isdir (dirname2):
print
"Success"
3. win32file.CopyFile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import
os
import
win32file
import
tempfile
filename1
=
tempfile.mktemp (
".txt"
)
open
(filename1,
"w"
).close ()
filename2
=
filename1
+
".copy"
print
filename1,
"=>"
, filename2
#拷文件
#文件已存在时,1为不覆盖,0为覆盖
win32file.CopyFile (filename1, filename2,
1
)
win32file.CopyFile (filename1, filename2,
0
)
win32file.CopyFile (filename1, filename2,
1
)
if
os.path.isfile (filename2):
print
"Success"
dirname1
=
tempfile.mktemp (
".dir"
)
os.mkdir (dirname1)
dirname2
=
dirname1
+
".copy"
print
dirname1,
"=>"
, dirname2
#拷目录
win32file.CopyFile (dirname1, dirname2,
1
)
if
os.path.isdir (dirname2):
print
"Success"
4. SHFileOperation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import
os
from
win32com.shell
import
shell, shellcon
import
tempfile
filename1
=
tempfile.mktemp (
".txt"
)
open
(filename1,
"w"
).close ()
filename2
=
filename1
+
".copy"
print
filename1,
"=>"
, filename2
#拷文件
#文件已存在时,shellcon.FOF_RENAMEONCOLLISION会指示重命名文件
shell.SHFileOperation (
(
0
, shellcon.FO_COPY, filename1, filename2,
0
,
None
,
None
)
)
shell.SHFileOperation (
(
0
, shellcon.FO_COPY, filename1, filename2, shellcon.FOF_RENAMEONCOLLISION,
None
,
None
)
)
shell.SHFileOperation (
(
0
, shellcon.FO_COPY, filename1, filename2,
0
,
None
,
None
)
)
if
os.path.isfile (filename2):
print
"Success"
dirname1
=
tempfile.mktemp (
".dir"
)
os.mkdir (dirname1)
dirname2
=
dirname1
+
".copy"
print
dirname1,
"=>"
, dirname2
#拷目录
shell.SHFileOperation (
(
0
, shellcon.FO_COPY, dirname1, dirname2,
0
,
None
,
None
)
)
if
os.path.isdir (dirname2):
print
"Success"
- Windows下用Python你会几种copy文件的方法?
- Windows下用Python你会几种copy文件的方法-Python shutil模块
- Windows下用Python你会几种copy文件的方法以及效率分析
- Copy文件的方法
- python的copy.copy()和copy.deepcopy()方法
- 【Windows】Windows 8.1系统下的控制台命令COPY实现文件的复制
- Python监控Windows下的文件变化
- Windows下安装Python模块的方法
- windows下python运行的方法
- Windows下 Python 模块的安装方法
- Windows下 Python 模块的安装方法
- windows下删除顽固文件的方法
- Windows下创建.htaccess文件的方法
- Python 文件的Copy and Paste
- Python 的set 类型及其copy方法
- Python中列表的copy方法
- Python中字典的copy方法总结
- python sharing your code windows下分享你的代码
- 对人脑记忆的探索
- Nhibernate: Foreign key must have same number of columns as the referenced primary key
- Python目录操作
- sencha touch 打包android应用教程
- 【mysql】mysql cluster搭建
- Windows下用Python你会几种copy文件的方法?
- Python 复制文件操作
- Python文件复制中如何复制相关文件的实际操作
- Oracle 10g Shrink Table 详解[表空间收缩]
- Python 操作文件及目录
- MySQL性能优化的最佳21条经验
- 金额的正则表达式
- 爱情的经济学分析
- 1060 Is It A Tree?