1. [Code]1. Os.system?
12345678910111213141516171819202122 |
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. [Code]2. Shutil.copy and Shutil.copytree?
1234567891011121314151617181920212223 |
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. [Code]3. Win32file. CopyFile?
1234567891011121314151617181920212223242526 |
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. [Code]4. SHFileOperation?
12345678910111213141516171819202122232425262728293031323334 |
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"
|
Appendix:
The operation of files, folders (file manipulation functions) in Python involves both the OS module and the Shutil module.
Get the current working directory, that is, the directory path of the current Python script work: OS.GETCWD ()
Returns all files and directory names under the specified directory name:os.listdir ()
function to delete a file:os.remove ()
Delete multiple directories:os.removedirs (r "C:\python")
Verifies whether the given path is a file:os.path.isfile ()
Verify that the given path is a directory:os.path.isdir ()
Determine if it is an absolute path:os.path.isabs ()
Verify that the given path is really saved:os.path.exists ()
Returns the directory name and file name of a path:os.path.split () eg os.path.split ('/home/swaroop/byte/code/poem.txt ') Result: ('/home/swaroop/ Byte/code ', ' poem.txt ')
Detach extension:os.path.splitext ()
Get path name:os.path.dirname ()
Get file name:os.path.basename ()
Run shell command: os.system ()
Read and SET environment variables:os.getenv () and os.putenv ()
Gives the line terminator used by the current platform:os.linesep Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '
Indicates the platform you are using:os.name for Windows, it is ' NT ', and for Linux/unix users, it is ' POSIX '
Rename:os.rename (old, new)
Create a multilevel directory:os.makedirs (r "C:\python\test")
Create a single directory:os.mkdir ("test")
Get file attributes:os.stat (file)
Modify file permissions and timestamps:os.chmod (file)
Terminate current process:os.exit ()
Get file Size:os.path.getsize (filename)
File operation:
Os.mknod ("test.txt") Create an empty file
fp = open ("Test.txt", W) opens a file directly and creates a file if the file does not exist
About open mode:
W opens in write mode,
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read/write mode (see W)
A + opens in read/write mode (see a)
RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)
fp.read ([size]) #size为读取的长度, in bytes
fp.readline ([size]) #读一行, if size is defined, it is possible to return only part of a row
fp.readlines ([size]) #把文件每一行作为一个list的一个成员 and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file.
fp.write (str) #把str写到文件中, write () does not add a newline character after Str
fp.writelines (seq) #把seq的内容全部写到文件中 (multi-line write-once). This function simply writes faithfully and does not add anything behind each line.
fp.close () #关闭文件. Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates VALUEERROR
Fp.flush () #把缓冲区的内容写入硬盘
Fp.fileno () #返回一个长整型的 "file label"
Fp.isatty () #文件是否是一个终端设备文件 (on UNIX systems)
Fp.tell () #返回文件操作标记的当前位置, starting with the origin of the file
fp.next () #返回下一行 and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.
Fp.seek (offset[,whence]) #将文件打操作标记移到offset的位置. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.
fp.truncate ([size]) #把文件裁成规定的大小, the default is the location that is cropped to the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.
Directory operation:
Os.mkdir ("file") Create directory
Copy file:
shutil.copyfile ("Oldfile", "NewFile") Oldfile and newfile can only be files
shutil.copy ("Oldfile", "NewFile") Oldfile can only be a folder, NewFile may be a file, or it can be a destination directory
Copy folder:
shutil.copytree ("Olddir", "Newdir") Olddir and Newdir can only be directories, and newdir must not exist
Rename file (directory)
os.rename ("Oldname", "newname") files or directories are
moving files (directories)
shutil.move ("Oldpos", "Newpos") using this command
Delete file
os.remove ("file")
Delete directory
os.rmdir ("dir") can only delete empty directories
shutil.rmtree ("dir") Empty directory, content directory can be deleted
Convert directory
os.chdir ("path") Change path
How do you make several copy files with Python under Windows?