Python对文件的基本操作
本人简要介绍了python对文件的基本操作命令,包括判断文件路径是否存在,打开文件,读取文件,写入文件,关闭文件。对文件内容的处理,比如查找字符,删除相关字符等可以参考正则方面的文章。
1.Python命令: open, read, readline, readlines, string.split, write, close
2.代码及部分注释。
import os.path
import string
if os.path.exists("D:/python/learning_with_python"):# judge if the given path is exist
print "path D:\python\learning_with_python is exist "
else:
print "path D:\python\learning_with_python is NO exist "
if os.path.isfile("D:/python/learning_with_python/common_python_module/forfileoperation.txt"):# judge if the file is exist
print "file forfileoperation.txt is exist"
else:
print "file forfileoperation.txt is NO exist "
fileopen = open("D:/python/learning_with_python/common_python_module/forfileoperation.txt", 'r')# open a file, please be aware path need use '/' but not '\'
message = fileopen.read()
print message
·输出
>>> import os.path
>>> import string
>>> if os.path.exists("D:/python/learning_with_python"):
print "path D:\python\learning_with_python is exist "
else:
print "path D:\python\learning_with_python is NO exist "
path D:\python\learning_with_python is exist
>>> if os.path.isfile("D:/python/learning_with_python/common_python_module/forfileoperation.txt"):
print "file forfileoperation.txt is exist"
else:
print "file forfileoperation.txt is NO exist "
file forfileoperation.txt is exist
>>> fileopen = open("D:/python/learning_with_python/common_python_module/forfileoperation.txt", 'r')
>>> message = fileopen.read()
>>> print message
line1: Do not allow your dog to be fat
line2: Do not take your dog for a ride in the car without securing him.
line3:Do not let your dog off leash in open areas
line4: Do not let your dog play with young children without proper supervision
line5: Do not give "people meds" to your dog
paragraph1
1. Do not allow your dog to be fat. I realize you hear this all the time, but there's a reason why we veterinarians will not let this issue go.
paragraph2
2. Do not take your dog for a ride in the car without securing him. A loose dog can be a distraction to a driver, and in an accident, the dog can become a projectile, injuring himself or others in the car, possibly seriously. There are plenty of canine restraint products on the market, but many won't protect you or your dog much.
paragraph3
3. Do not let your dog off leash in open areas. Very few dogs heel reliably off lead, and just as few - if not fewer - have a foolproof emergency recall.
paragraph4
4. Do not let your dog play with young children without proper supervision. I'm borrowing this one from my daughter, Vetstreet training expert Mikkel Becker.
paragraph5
5. Do not give "people meds" to your dog without checking with your veterinarian first. My Vetstreet colleague Dr. Patty Khuly has written about dog-safe medications and how to use them.
fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'w')# open a file in mode of write
fp.write(message)# write message to the file
fp.close()
fileopen.close()
fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'r') # open a file in mode of read
copy_read = fp.read() # read the openned file, and return as a string
print "the type of read() is a string,%s" ,type(copy_read)
print copy_read# print the string
readlist = copy_read.split("paragraph")# use "paragraph" to split the string copy_read into a list
print readlist
fp.close()
·输出
>>> fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'w')
>>> fp.write(message)
>>> fp.close()
>>> fileopen.close()
>>> fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'r')
>>> copy_read = fp.read()
>>> print copy_read
line1: Do not allow your dog to be fat
line2: Do not take your dog for a ride in the car without securing him.
line3:Do not let your dog off leash in open areas
line4: Do not let your dog play with young children without proper supervision
line5: Do not give "people meds" to your dog
paragraph1
1. Do not allow your dog to be fat. I realize you hear this all the time, but there's a reason why we veterinarians will not let this issue go.
paragraph2
2. Do not take your dog for a ride in the car without securing him. A loose dog can be a distraction to a driver, and in an accident, the dog can become a projectile, injuring himself or others in the car, possibly seriously. There are plenty of canine restraint products on the market, but many won't protect you or your dog much.
paragraph3
3. Do not let your dog off leash in open areas. Very few dogs heel reliably off lead, and just as few - if not fewer - have a foolproof emergency recall.
paragraph4
4. Do not let your dog play with young children without proper supervision. I'm borrowing this one from my daughter, Vetstreet training expert Mikkel Becker.
paragraph5
5. Do not give "people meds" to your dog without checking with your veterinarian first. My Vetstreet colleague Dr. Patty Khuly has written about dog-safe medications and how to use them.
>>> print "the type of read() is a string,%s" ,type(a_copy)
the type of read() is a string,%s
>>> readlist = copy_read.split("paragraph")
>>> print readlist
['line1: Do not allow your dog to be fat\nline2: Do not take your dog for a ride in the car without securing him. \nline3:Do not let your dog off leash in open areas\nline4: Do not let your dog play with young children without proper supervision\nline5: Do not give "people meds" to your dog \n\n', "1\n1. Do not allow your dog to be fat. I realize you hear this all the time, but there's a reason why we veterinarians will not let this issue go. \n\n\n", "2\n\n\n2. Do not take your dog for a ride in the car without securing him. A loose dog can be a distraction to a driver, and in an accident, the dog can become a projectile, injuring himself or others in the car, possibly seriously. There are plenty of canine restraint products on the market, but many won't protect you or your dog much. \n\n", '3\n3. Do not let your dog off leash in open areas. Very few dogs heel reliably off lead, and just as few - if not fewer - have a foolproof emergency recall. \n\n\n', "4\n4. Do not let your dog play with young children without proper supervision. I'm borrowing this one from my daughter, Vetstreet training expert Mikkel Becker. \n\n\n", '5\n5. Do not give "people meds" to your dog without checking with your veterinarian first. My Vetstreet colleague Dr. Patty Khuly has written about dog-safe medications and how to use them. \n\n']
>>> print len(readlist)
6
>>> fp.close()
fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'r') # open a file in mode of read
copy_readline1 = fp.readline()# read only one line of openned file, and return as a string
print "the type of readline() is a string,%s" ,type(copy_readline1)
print copy_readline1# print the contant
copy_readline1 = fp.readline()# read the 2nd line of openned file and return as a string
print copy_readline1
fp.close()
·输出
>>> fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'r')
>>> copy_readline1 = fp.readline()
>>> print "the type of readline() is a string,%s" ,type(copy_readline1)
the type of readline() is a string,%s <type 'str'>
>>> print copy_readline1
line1: Do not allow your dog to be fat
>>> copy_readline1 = fp.readline()
>>> print copy_readline1
line2: Do not take your dog for a ride in the car without securing him.
>>> fp.close()
>>>
fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'r') # open a file in mode of read
copy_readlines = fp.readlines() # read the whole openned file, and return as a list
print "the type of readline() is a list,%s" ,type(copy_readlines)
print copy_readlines
fp.close()
·输出
>>> fp = open("D:/python/learning_with_python/common_python_module/tmp.txt", 'r')
>>> copy_readlines = fp.readlines()
>>> print "the type of readline() is a list,%s" ,type(copy_readlines)
the type of readline() is a list,%s <type 'list'>
>>> print copy_readlines
['line1: Do not allow your dog to be fat\n', 'line2: Do not take your dog for a ride in the car without securing him. \n', 'line3:Do not let your dog off leash in open areas\n', 'line4: Do not let your dog play with young children without proper supervision\n', 'line5: Do not give "people meds" to your dog \n', '\n', 'paragraph1\n', "1. Do not allow your dog to be fat. I realize you hear this all the time, but there's a reason why we veterinarians will not let this issue go. \n", '\n', '\n', 'paragraph2\n', '\n', '\n', "2. Do not take your dog for a ride in the car without securing him. A loose dog can be a distraction to a driver, and in an accident, the dog can become a projectile, injuring himself or others in the car, possibly seriously. There are plenty of canine restraint products on the market, but many won't protect you or your dog much. \n", '\n', 'paragraph3\n', '3. Do not let your dog off leash in open areas. Very few dogs heel reliably off lead, and just as few - if not fewer - have a foolproof emergency recall. \n', '\n', '\n', 'paragraph4\n', "4. Do not let your dog play with young children without proper supervision. I'm borrowing this one from my daughter, Vetstreet training expert Mikkel Becker. \n", '\n', '\n', 'paragraph5\n', '5. Do not give "people meds" to your dog without checking with your veterinarian first. My Vetstreet colleague Dr. Patty Khuly has written about dog-safe medications and how to use them. \n', '\n']
>>> print len(copy_readlines)
26
>>> fp.close()
>>>
3.注意点
oread, readline, readlines 的区别:
·read 是读整个文件并以字符串形式返回,
·readline是每次读一行并以字符串的形式返回,
·readlines是读整个文件并以列表形式返回。
o文件open以后记得close。