python file operate
来源:互联网 发布:filterpro 软件下载 编辑:程序博客网 时间:2024/05/17 00:09
同学之间常常要共享精彩电影的,一般电影的大小都是300多M,我有个128M的小MP3,总向同学借大U盘也不是那么回事,有时就得耐着性子用winrar分割压缩,传到自己电脑里再解压缩合并成一个电影文件。用的次数多了大家就都会知道winrar对音乐和电影这些多媒体格式的文件压缩率基本为0,也就是说,即使在那浪费时间浪费计算机资源(非常浪费CPU)最后的结果也只是完成了分割与合并文件的作用,大量的计算做的都是无用功。
用Pythong写了个简单的文件分割与合并的程序,这样自己的小容量MP3用起来就方便了,而且因为不需要压缩与解压缩的过程,可以大量节省我的耐心。
file.py
01
import
sys
02
from
os.path
import
exists
03
fileCount
=
0
04
05
def
splitFile(fileName, partSize
=
1
):
06
# 1024 * 1024 = 1048576
07
length
=
partSize
*
1048576
08
f1
=
open
(fileName,
"rb"
)
09
while
True
:
10
content
=
f1.read(length)
11
if
content
=
=
"":
12
break
13
newFile
=
distFile(fileName)
14
f2
=
open
(newFile,
"wb"
)
15
f2.write(content)
16
f2.close()
17
f1.close()
18
print
'split file complete!'
19
20
21
22
def
distFile(sourceFile):
23
global
fileCount
24
fileCount
=
fileCount
+
1
25
extPos
=
sourceFile.rfind(
'.'
)
26
if
extPos >
0
:
27
return
sourceFile
+
'.part'
+
str
(fileCount)
28
else
:
# extPos == -1
29
print
'File type? Can not split!'
30
sys.exit(
1
)
31
32
def
combine(filename):
33
count
=
0
34
extPos
=
filename.find(
'.part'
)
35
if
extPos >
0
:
36
file
=
filename[:extPos]
37
f1
=
open
(
file
,
"wb"
)
38
while
True
:
39
count
=
count
+
1
40
partFile
=
file
+
'.part'
+
str
(count)
41
if
not
exists(partFile):
42
break
43
else
:
44
f2
=
open
(partFile,
"rb"
)
45
content
=
f2.read()
46
f2.close()
47
f1.write(content)
48
f1.close()
49
print
'combine file complete!'
50
51
else
:
52
print
'File type? Can not combine!'
53
54
def
usage():
55
print
'''usage is "file.py s[c] filename" or "file.py s[c] filename size(M) /n 's' -- 'split', 'c' -- 'combine" '''
56
57
58
if
__name__
=
=
"__main__"
:
59
if
len
(sys.argv) !
=
3
and
len
(sys.argv) !
=
4
:
60
usage()
61
sys.exit(
1
)
62
if
sys.argv[
1
]
=
=
's'
:
63
if
len
(sys.argv)
=
=
3
:
64
splitFile(sys.argv[
2
])
65
elif
len
(sys.argv)
=
=
4
and
int
(sys.argv[
3
]) >
0
:
66
splitFile(sys.argv[
2
],
int
(sys.argv[
3
]))
67
else
:
68
usage()
69
sys.exit(
1
)
70
71
elif
sys.argv[
1
]
=
=
'c'
:
72
if
len
(sys.argv)
=
=
3
:
73
combine(sys.argv[
2
])
74
else
:
75
usage()
76
else
:
77
usage()
使用方法:
分割:file.py s 文件路径 每块的文件大小(不填默认为1M)
合并:file.py c 任意部分文件路径
因为用winrar解压缩时双击任何一个部分压缩文件都会成功解压该完成文件而不必只能点击part1,所以这个程序也写成给出任何一个部分文件名都可以合并处完整的文件,这样就可以方便的在命令行随便用鼠标拖入一个分割后的部分文件就可以了。估计winrar的.rar 格式文件里面是包含了压缩信息和共有多少个部分文件的,我的这种自己用的小程序就得人工判断分割成了几部分然后全部copy了。
再用py2exe转换成.exe文件装入U盘就可以了,以后到谁的机器上都可以用了。
转自: http://lulinbest.blog.sohu.com/62764871.html
用Python进行文件操作是比较简单的,在Python中file是内置类型之一,内置的函数open、file都可以创建file对象,创建好之后就可以对其进行读写等操作。
近几天看Python Programing 3rd ,觉得书很不错。
文件分割的原理很简单:以二进制形式打开文件流,按照指定的大小读取,然后写入新文件。
文件合并的原理正好相反。
下面的代码摘自PP3rd里面。
split_file.py
01
#!/usr/bin/python
02
##########################################################################
03
# split a file into a set of parts; join.py puts them back together;
04
# this is a customizable version of the standard unix split command-line
05
# utility; because it is written in Python, it also works on Windows and
06
# can be easily modified; because it exports a function, its logic can
07
# also be imported and reused in other applications;
08
##########################################################################
09
10
import
sys, os
11
kilobytes
=
1024
12
megabytes
=
kilobytes
*
1000
13
chunksize
=
int
(
1.4
*
megabytes)
# default: roughly a floppy
14
15
def
split(fromfile, todir, chunksize
=
chunksize):
16
if
not
os.path.exists(todir):
# caller handles errors
17
os.mkdir(todir)
# make dir, read/write parts
18
else
:
19
for
fname
in
os.listdir(todir):
# delete any existing files
20
os.remove(os.path.join(todir, fname))
21
partnum
=
0
22
input
=
open
(fromfile,
'rb'
)
# use binary mode on Windows
23
while
1
:
# eof=empty string from read
24
chunk
=
input
.read(chunksize)
# get next part <= chunksize
25
if
not
chunk:
break
26
partnum
=
partnum
+
1
27
filename
=
os.path.join(todir, (
'part%04d'
%
partnum))
28
fileobj
=
open
(filename,
'wb'
)
29
fileobj.write(chunk)
30
fileobj.close()
# or simply open().write()
31
input
.close()
32
assert
partnum <
=
9999
# join sort fails if 5 digits
33
return
partnum
34
35
if
__name__
=
=
'__main__'
:
36
if
len
(sys.argv)
=
=
2
and
sys.argv[
1
]
=
=
'-help'
:
37
print
'Use: split.py [file-to-split target-dir [chunksize]]'
38
else
:
39
if
len
(sys.argv) <
3
:
40
interactive
=
1
41
fromfile
=
raw_input
(
'File to be split? '
)
# input if clicked
42
todir
=
raw_input
(
'Directory to store part files? '
)
43
else
:
44
interactive
=
0
45
fromfile, todir
=
sys.argv[
1
:
3
]
# args in cmdline
46
if
len
(sys.argv)
=
=
4
: chunksize
=
int
(sys.argv[
3
])
47
absfrom, absto
=
map
(os.path.abspath, [fromfile, todir])
48
print
'Splitting'
, absfrom,
'to'
, absto,
'by'
, chunksize
49
50
try
:
51
parts
=
split(fromfile, todir, chunksize)
52
except
:
53
print
'Error during split:'
54
print
sys.exc_info()[
0
], sys.exc_info()[
1
]
55
else
:
56
print
'Split finished:'
, parts,
'parts are in'
, absto
57
if
interactive:
raw_input
(
'Press Enter key'
)
# pause if clicked
join_file.py
01
#!/usr/bin/python
02
##########################################################################
03
# join all part files in a dir created by split.py, to recreate file.
04
# This is roughly like a 'cat fromdir/* > tofile' command on unix, but is
05
# more portable and configurable, and exports the join operation as a
06
# reusable function. Relies on sort order of file names: must be same
07
# length. Could extend split/join to popup Tkinter file selectors.
08
##########################################################################
09
10
import
os, sys
11
readsize
=
1024
12
13
def
join(fromdir, tofile):
14
output
=
open
(tofile,
'wb'
)
15
parts
=
os.listdir(fromdir)
16
parts.sort()
17
for
filename
in
parts:
18
filepath
=
os.path.join(fromdir, filename)
19
fileobj
=
open
(filepath,
'rb'
)
20
while
1
:
21
filebytes
=
fileobj.read(readsize)
22
if
not
filebytes:
break
23
output.write(filebytes)
24
fileobj.close()
25
output.close()
26
27
if
__name__
=
=
'__main__'
:
28
if
len
(sys.argv)
=
=
2
and
sys.argv[
1
]
=
=
'-help'
:
29
print
'Use: join.py [from-dir-name to-file-name]'
30
else
:
31
if
len
(sys.argv) !
=
3
:
32
interactive
=
1
33
fromdir
=
raw_input
(
'Directory containing part files? '
)
34
tofile
=
raw_input
(
'Name of file to be recreated? '
)
35
else
:
36
interactive
=
0
37
fromdir, tofile
=
sys.argv[
1
:]
38
absfrom, absto
=
map
(os.path.abspath, [fromdir, tofile])
39
print
'Joining'
, absfrom,
'to make'
, absto
40
41
try
:
42
join(fromdir, tofile)
43
except
:
44
print
'Error joining files:'
45
print
sys.exc_info()[
0
], sys.exc_info()[
1
]
46
else
:
47
print
'Join complete: see'
, absto
48
if
interactive:
raw_input
(
'Press Enter key'
)
# pause if clicked
以上代码在window下面测试成功,可以分割文件文件已经任何二进制文件。
转自: http://blog.csdn.net/foyuan/archive/2007/08/01/1720600.aspx
--End--
- python file operate
- file operate
- file operate in python (open write read close )
- C# Operate Excel File
- C# Operate Excel File
- C# Operate Excel File
- Jscript file operate
- C++ FILE OPERATE
- linux file operate
- operate
- java file 文件操作 operate file of java
- python operate excel with pywin com
- The Classical Example using DOM to operate a XML File
- how to access and operate a binarry file ?
- Operate SSDBGrid
- operate layout
- property operate
- windows operate
- python 文件操作
- 转:Windows Perl 配置及其 Movable Type 安装笔记
- MmIsAddressValid
- 动态规划;最大子段和;温故而知新-。-;这个动规公式找的很聪明;
- 披着“云”衣裳的狗——搜狗输入法“云”版本尝鲜记
- python file operate
- linux 安装oracle 报错提示error in invoking target ntcontab.o of makefile
- 看到97M的文件取下来了,我感到自己责任的重大
- 将指定文件夹下结构相同的EXCEL文件批量导入到SQL数据库中指定的表
- POJ 2250Compromise(DP最长子公共子序列)
- QTableView表格中出现多余的Checkbox
- jsp乱码学习
- AdRotator编写轮换广告图片
- 面向对象的开发方法