Python实现给文件添加内容及得到文件信息的方法

2024-05-02

Python实现给文件添加内容及得到文件信息的方法(精选2篇)

篇1:Python实现给文件添加内容及得到文件信息的方法

作者:wayne92 字体:[增加 减小] 类型:

这篇文章主要介绍了Python实现给文件添加内容及得到文件信息的方法,可实现从文件开头添加内容的功能,需要的朋友可以参考下

本文实例讲述了Python实现给文件添加内容及得到文件信息的方法,分享给大家供大家参考。具体分析如下:

经常会遇到给文件添加内容的时候,如果只是添加在文件的末尾,就比较简单了:

file = open(filename,‘a‘)file.write(‘hello‘)file.close

使用‘a‘模式打开文件后,指针默认指向文件末尾,即使你:

file.seek(0)file.write(‘world‘)

字符串‘world‘还是会加在文件的末尾,而不会是你想要的开始位置。

而我遇到的需求就是要在文件头添加东西啊,怎么办呢?不至于把里面东西全读出来,再写进去吧?

还好看到了‘r+‘这个模式(以前从来没有用过)

file = open(filename,‘r+‘)file.tell() #0Lfile.write(‘begin‘)file.close()

打开文件看看,是不是可以了呢;)

得到文件的修改时间:

>>> t = os.path.getmtime(path)>>> t1190626843>>> type(t)>>> os.stat(path)[8]1190626843

得到文件的大小:

>>> os.stat(path)[6]2808L>>> os.path.getsize(path)2808L

希望本文所述对大家的Python程序设计有所帮助,

篇2:Python实现给文件添加内容及得到文件信息的方法

这篇文章主要介绍了Python实现分割文件及合并文件的方法,涉及Python针对文件的分割与合并操作相关技巧,通过自定义函数split与join实现了文件的分割与合并操作,需要的朋友可以参考下

本文实例讲述了Python实现分割文件及合并文件的方法,分享给大家供大家参考。具体如下:

分割文件split.py如下:

#!/usr/bin/python########################################################################### split a file into a set of parts; join.py puts them back together;# this is a customizable version of the standard unix split command-line # utility; because it is written in Python, it also works on Windows and# can be easily modified; because it exports a function, its logic can # also be imported and reused in other applications;##########################################################################import sys, skilobytes = 1024megabytes = kilobytes * 1000chunksize = int(1.4 * megabytes) # default: roughly a floppydef split(fromfile, todir, chunksize=chunksize): if not os.path.exists(todir): # caller handles errors os.mkdir(todir) # make dir, read/write parts else: for fname in os.listdir(todir): # delete any existing files os.remove(os.path.join(todir, fname)) partnum = 0 input = open(fromfile, ‘rb‘) # use binary mode on Windows while 1: # eof=empty string from read chunk = input.read(chunksize) # get next part <= chunksize if not chunk: break partnum = partnum+1 filename = os.path.join(todir, (‘part%04d‘ % partnum)) fileobj = open(filename, ‘wb‘) fileobj.write(chunk) fileobj.close # or simply open().write() input.close() assert partnum <= 9999 # join sort fails if 5 digits return partnumif __name__ == ‘__main__‘: if len(sys.argv) == 2 and sys.argv[1] == ‘-help‘: print ‘Use: split.py [file-to-split target-dir [chunksize]]‘ else: if len(sys.argv) < 3: interactive = 1 fromfile = raw_input(‘File to be split? ‘) # input if clicked todir = raw_input(‘Directory to store part files? ‘) else: interactive = 0 fromfile, todir = sys.argv[1:3] # args in cmdline if len(sys.argv) == 4: chunksize = int(sys.argv[3]) absfrom, absto = map(os.path.abspath, [fromfile, todir]) print ‘Splitting‘, absfrom, ‘to‘, absto, ‘by‘, chunksize try: parts = split(fromfile, todir, chunksize) except: print ‘Error during split:‘ print sys.exc_info()[0], sys.exc_info()[1] else: print ‘Split finished:‘, parts, ‘parts are in‘, absto if interactive: raw_input(‘Press Enter key‘) # pause if clicked

合并文件join_file.py如下:

#!/usr/bin/python########################################################################### join all part files in a dir created by split.py, to recreate file. # This is roughly like a ‘cat fromdir/* > tofile‘ command on unix, but is # more portable and configurable, and exports the join operation as a # reusable function. Relies on sort order of file names: must be same # length. Could extend split/join to popup Tkinter file selectors.##########################################################################import os, sysreadsize = 1024def join(fromdir, tofile): utput = open(tofile, ‘wb‘) parts = os.listdir(fromdir) parts.sort() for filename in parts: filepath = os.path.join(fromdir, filename) fileobj = open(filepath, ‘rb‘) while 1: filebytes = fileobj.read(readsize) if not filebytes: break output.write(filebytes) fileobj.close() output.close()if __name__ == ‘__main__‘: if len(sys.argv) == 2 and sys.argv[1] == ‘-help‘: print ‘Use: join.py [from-dir-name to-file-name]‘ else: if len(sys.argv) != 3: interactive = 1 fromdir = raw_input(‘Directory containing part files? ‘) tofile = raw_input(‘Name of file to be recreated? ‘) else: interactive = 0 fromdir, tofile = sys.argv[1:] absfrom, absto = map(os.path.abspath, [fromdir, tofile]) print ‘Joining‘, absfrom, ‘to make‘, absto try: join(fromdir, tofile) except: print ‘Error joining files:‘ print sys.exc_info()[0], sys.exc_info()[1] else: print ‘Join complete: see‘, absto if interactive: raw_input(‘Press Enter key‘) # pause if clicked

上一篇:商务邮件――邀请客户参加周年庆典下一篇:春泥计划