- 浏览: 1241965 次
文章分类
最新评论
python之文件管理
File Management in Python
(Page 1 of 5 )
File management is a basic function, and an important part of many applications. Python makes file management surprisingly easy, especially when compared to other languages. Peyton McCullough explains the basics.
(Page 1 of 5 )
File management is a basic function, and an important part of many applications. Python makes file management surprisingly easy, especially when compared to other languages. Peyton McCullough explains the basics.
Introduction
The game you played yesterday uses files to store game saves. The order you placed yesterday was saved in a file. That report you typed up this morning was, obviously, stored in a file as well.
File management is an important part of many applications written in nearly every language. Python is no exception to this. In this article, we will explore the task of manipulating files using several modules. We'll read, write to, append and do other strange things to files. Let's get started.
Reading and Writing
The most basic tasks involved in file manipulation are reading data from files and writing data to files. This is a very simple task to learn. Let's open a file for writing:
fileHandle = open ( 'test.txt', 'w' )
The "w" indicates that we will be writing to the file, and the rest is pretty simple to understand. The next step is to write data to the file:
fileHandle.write ( 'This is a test./nReally, it is.' )
This will write the string "This is a test." to the file's first line and "Really, it is." to the file's second line. Finally, we need to clean up after ourselves and close the file:
fileHandle.close()
As you can see, it's very easy, especially with Python's object orientation. Note that when you use the "w" mode to write to the file again, all its contents will be deleted. To get past this, use the "a" mode to append data to a file, adding data to the bottom:
fileHandle = open ( 'test.txt', 'a' )fileHandle.write ( '/n/n/nBottom line.' )fileHandle.close()
Now let's read our file and display the contents:
fileHandle = open ( 'test.txt' )print fileHandle.read()fileHandle.close()
This will read the entire file and print the data within it. We can also read a single line in the file:
fileHandle = open ( 'test.txt' )print fileHandle.readline() # "This is a test."fileHandle.close()
It is also possible to store the lines of a file into a list:
fileHandle = open ( 'test.txt' )fileList = fileHandle.readlines()for fileLine in fileList:print '>>', fileLinefileHandle.close()
When reading a file, Python's place in the file will be remembered, illustrated in this example:
fileHandle = open ( 'test.txt' )garbage = fileHandle.readline()fileHandle.readline() # "Really, it is."fileHandle.close()
Only the second line is displayed. We can, however, get past this by telling Python to resume reading from a different position:
fileHandle = open ( 'test.txt' )garbage = fileHandle.readline()fileHandle.seek ( 0 )print fileHandle.readline() # "This is a test."fileHandle.close()
In the above example, we tell Python to continue reading from the first byte in the file. Thus, the first line is printed. We can also request Python's place within the file:
fileHandle = open ( 'test.txt' )print fileHandle.readline() # "This is a test."print fileHandle.tell() # "17"print fileHandle.readline() # "Really, it is."
It is also possible to read the file a few bytes at a time:
fileHandle = open ( 'test.txt' )print fileHandle.read ( 1 ) # "T"fileHandle.seek ( 4 )print FileHandle.read ( 1 ) # "T"
When working with Windows and Macintosh, sometimes you are required to read and write files in binary mode, such as images or executional files. To do this, simply append "b" to the file mode:
fileHandle = open ( 'testBinary.txt', 'wb' )fileHandle.write ( 'There is no spoon.' )fileHandle.close()fileHandle = open ( 'testBinary.txt', 'rb' )print fileHandle.read()fileHandle.close()
Using several of Python's modules, it is possible to obtain information on existig files. To get basic information, the "os" module can be used in conjunction with the "stat" module:
import osimport statimport timefileStats = os.stat ( 'test.txt' )fileInfo = {'Size' : fileStats [ stat.ST_SIZE ],'LastModified' : time.ctime ( fileStats [ stat.ST_MTIME ] ),'LastAccessed' : time.ctime ( fileStats [ stat.ST_ATIME ] ),'CreationTime' : time.ctime ( fileStats [ stat.ST_CTIME ] ),'Mode' : fileStats [ stat.ST_MODE ]}for infoField, infoValue in fileInfo:print infoField, ':' + infoValueif stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):print 'Directory. 'else:print 'Non-directory.'
The above example creates a dictionary containing some basic information about the file. It then displays the data and tells us if it's a directory or not. We can also check to see if the file is one of several other types:
import osimport statfileStats = os.stat ( 'test.txt' )fileMode = fileStats [ stat.ST_MODE ]if stat.S_ISREG ( fileStats [ stat.ST_MODE ] ):print 'Regular file.'elif stat.S_ISDIR ( fileSTats [ stat.ST_MODe ] ):print 'Directory.'elif stat.S_ISLNK ( fileSTats [ stat.ST_MODe ] ):print 'Shortcut.'elif stat.S_ISSOCK ( fileSTats [ stat.ST_MODe ] ):print 'Socket.'elif stat.S_ISFIFO ( fileSTats [ stat.ST_MODe ] ):print 'Named pipe.'elif stat.S_ISBLK ( fileSTats [ stat.ST_MODe ] ):print 'Block special device.'elif stat.S_ISCHR ( fileSTats [ stat.ST_MODe ] ):print 'Character special device.'
Additionally, we canuse "os.path" to gather basic information:
import os.pathfileStats = 'test.txt'if os.path.isdir ( fileStats ):print 'Directory.'elif os.path.isfile ( fileStats ):print 'File.'elif os.path.islink ( fileStats ):print 'Shortcut.'elif os.path.ismount ( fileStats ):print 'Mount point.'
File Management in Python - Directories
(Page 3 of 5 )
(Page 3 of 5 )
Directories, like regular files, are easy to work with. Let's start by listing the contents of a directory:
import osfor fileName in os.listdir ( '/' ):print fileName
As you can see, this is extremely simple, and it can be done in three lines.
Creating a directory is also simple:
import osos.mkdir ( 'testDirectory' )
It is equally as easy to delete the directory we just created:
import osos.rmdir ( 'testDirectory )
We can also create multiple directories at a time:
import osos.makedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )
Assuming we add nothing to the directories we just created, we can also delete them all at once:
import osos.removedirs ( 'I/will/show/you/how/deep/the/rabbit/hole/goes' )
Suppose we want to perform a specific action when a specific file type is reached. This can easily be done with the "fnmatch" module. Let's print the contents of all the ".txt" files we encounter and print the filename of any ".exe" files we encounter:
import fnmatchimport osfor fileName in os.listdir ( '/' ):if fnmatch.fnmath ( fileName, '*.txt' ):print open ( fileName ).read()elif fnmatch.fnmatch ( fileName, '*.exe' ):print fileName
The asterisk character can represent any amount of characters. If we want to match just one character, we can use the question mark:
import fnmatchimport osfor fileName in os.listdir ( '/' ):if fnmatch.fnmatch ( fileName, '?.txt' ):print 'Text file.'
It is also possible to create a regular expression using the "fnmatch" module, matching filenames with the "re" module:
import fnmatchimport osimport refilePattern = fnmatch.translate ( '*.txt' )for fileName in os.listdir ( '/' ):if re.match ( filePattern, fileName ):print 'Text file.'
If we're just looking for one type of filename, it is a lot easier to use the "glob" module. Its patterns are similar to those used in "fnmatch":
import globfor fileName in glob.glob ( '*.txt' ):print 'Text file.'
It is also possible to use ranges of characters in the patterns, just as you would in regular expressions. Suppose you want to print the names of text files with one digit before the extension:
import globfor fileName in glob.glob ( '[0-9].txt' ):print fileName
The "glob" module makes use of the "fnmatch" module.
(Page 4 of 5 )
Using the methods covered in the previous section, it is possible to read strings from files and write strings to files. However, in some situations, you may need to pass other types of data, such as lists, tuples, dictionaries and other objects. In Python, this is possible through a method known as pickling. To pickle data, you would use the "pickle" module included in the standard library.
Let's start by pickling a short list of strings and integers:
import picklefileHandle = open ( 'pickleFile.txt', 'w' )testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]pickle.dump ( testList, fileHandle )fileHandle.close()
Unpickling the data is just as easy:
import picklefileHandle = open ( 'pickleFile.txt' )testList = pickle.load ( fileHandle )fileHandle.cloes()
We can also store more complex data:
import picklefileHandle = open ( 'pickleFile.txt', 'w' )testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', [ 1, 2, 7 ] ]pickle.dump ( testList, fileHandle )fileHandle.close()import picklefileHandle = open ( 'pickleFile.txt' )testList = pickle.load ( fileHandle )fileHandle.close()
As you can see, pickling is extremely easy to do with Python's "pickle" module. Numeous objects may be stored in files with it. You can also use the "cPickle" module if it is availible to you. It's exactly the same as the "pickle" modue, but it's faster:
import cPicklefileHandle = open ( 'pickleFile.txt', 'w' )cPickle.dump ( 1776, fileHandle )fileHandle.close()
A number of modules you will encounter contain methods that require a file object as an argument. Sometimes, it is inconvenient to create and use a real file, however. Thankfully, you can create files that store themselves in a computer's memory using the "StringIO" module:
import StringIOfileHandle = StringIO.StringIO ( "Let freedom ring." )print fileHandle.read() # "Let freedom ring."fileHandle.close()
A "cStringIO" module is also availible. It is identical to the "StringIO" module in use, but, just like the "cPickle" module is to the "pickle" module, it is faster:
import cStringIOfileHandle = cStringIO.cStringIO ( "To Kill a Mockingbird" )print fileHandle.read() # "To Kill a Mockingbid"fileHandle.close()
Conclusion
File management is a task that programmers of many languages will often encounter in their applications. Thankfully, Python makes the task incredibly easy compared to other languages. It offers many modules in its standard library that aid programmers, and its object orientation further simplifies things.
You now have a basic understanding of file management in Python, and you will use it in many future applications.
DISCLAIMER: The content provided in this article is not warrantied or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation best practices. We are not liable for any negative consequences that may result by implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
相关推荐
Python基于Django框架的文档管理系统源码.zip
Python小型磁盘文件管理系统源码.zip
基于python与Flask的文件管理系统,主要功能有: 1、文件的上传和下载 2、消息发送和恢复 3、文件信息的增删改查 4、系统的登录和注册 使用的是mysql数据库,适合初学者下载使用。
python文件管理系统的各种版本 包含:初级版、函数版、文件版
python学生管理系统源码文件
学生信息管理系统(python+GUI+mysql).zip,学生信息管理系统(python+GUI+mysql).zip,学生信息管理系统(python+GUI+mysql).zip 学生信息管理系统(python+GUI+mysql).zip,学生信息管理系统(python+GUI+mysql).zip,...
python课程设计仓库管理系统,包含源码,文档,ppt 仓库管理系统应解决一些问题,例如:非常依赖人工经验,新手上手速度慢;对正在进行的现场作业的可见度不高;缺乏日常仓库作业报表,无法为管理者提供更多数据...
设计一个GUI界面的系统,模拟图书管理,一个面向学生和学校管理员的系统,图书信息以txt文件存在本地。 管理员:查询图书 增加图书 删除图书 学生:借阅图书 归还图书 任何一个操作都会将所更新的图书信息存入本地...
Python S60 上的一个文件管理器的源程序
Python学习文件管理
python课程设计学生成绩管理系统源码+汇报PPT。已拿高分,项目完整,注释清晰。 这次作业做的是学生python成绩管理系统,使用pyhton3.0实现了一个学生成绩管理系统,功能有:增加学生成绩、显示所有学生成绩、删除...
适合学生练习的python小demo
Python项目之django用Python设计学生成绩管理系统(源码 + 说明文档) 2 关键技术介绍 3 2.1Python语言简介 3 2.2Django框架 3 2.3MYSQL数据库 3 2.4B/S结构简介 4 3 需求分析与可行性分析 6 3.1功能需求分析 6 3.2...
由 Python Django 开发的结合文档管理,故障管理,服务器资产管理于一体的运维管理平台
python文件管理,上传的python文件管理代码,个人的一点学习资料
大学期间写的一份Python学生信息管理系统,使用的是文件存储的格式,但是没有学习数据库之类的东西,只能这样写了。代码包含:教师端学生端的登录,学生基本信息的增删改查等四项基本功能。教师端功能全,学生端把...
3、除了主程序program,其它文件默认放在python安装路径下;亦可修改代码路径 4、留了一个bug,厨师增加菜品如何同步到restaurant文件中?自己做一下 5、互相学习,有什么问题发QQ邮件2240928540
一个Python开发的小型磁盘文件管理系统源码.zip
Python学习文件管理2
毕业设计基于pythonWeb开发Django框架的图书商城管理系统源码。 软件架构:python的web开发Django框架 安装教程 在命令行中输入python manage.py runserver 运行商城。 注意:要在web里的目录下才能运行manage.py ...