Beginning Python From Novice to Professional (8) - 文件方法

文件方法

读写:

#!/usr/bin/env python
f = open('somefile.txt','w')
f.write('Hello,')
f.write('World!')
f.close()
f = open('somefile.txt','r')
print f.read(5)
Hello

使用基本文件方法:

#!/usr/bin/env python
f = open(r'somefile.txt')
print f.read()
f.close()
f = open(r'somefile.txt')
for i in range(3):
	print str(i) + ':' + f.readline()
f.close()
import pprint
pprint.pprint(open(r'somefile.txt').readlines())
f = open('somefile.txt','w')
f.write('we\nchange\nthis file!')
f.close()
f = open(r'somefile.txt')
print f.read()
f.close()
f = open(r'somefile.txt')
lines = f.readlines()
f.close()
lines[1] = "changed\n"
f = open(r'somefile.txt','w')
f.writelines(lines)
f.close()
f = open(r'somefile.txt')
print f.read()
f.close()
This
is a
Test!

0:This

1:is a

2:Test!

['This\n', 'is a\n', 'Test!\n']
we
change
this file!
we
changed
this file!

文件解包:

#!/usr/bin/env python
f = open(r'somefile.txt','w')
f.write('First line\n')
f.write('Second line\n')
f.write('Third line\n')
f.close()
lines = list(open('somefile.txt'))
print lines
first,second,third = open('somefile.txt')
print first
print second
print third
['First line\n', 'Second line\n', 'Third line\n']
First line

Second line

Third line
时间: 2024-10-12 14:26:08

Beginning Python From Novice to Professional (8) - 文件方法的相关文章

Beginning Python From Novice to Professional (2) - 命令行运行Python脚本

命令行运行Python脚本 Linux下先创建一个hello.py [python] view plaincopy $ gedit hello.py 输入: [python] view plaincopy #!/usr/bin/env python print 2+2 保存退出,运行: [python] view plaincopy $ python hello.py 4 我们也可以让它变得和普通程序一样执行 执行之前,让脚本文件具备可执行属性: [python] view plaincopy

Beginning Python From Novice to Professional (3) - 列表操作

列表操作 list函数: [python] view plaincopy >>> list('hello') ['h', 'e', 'l', 'l', 'o'] 改变列表: [python] view plaincopy >>> x=[1,1,1] >>> x[1]=2 >>> x [1, 2, 1] 删除元素: [python] view plaincopy >>> names = ['wu','li','zhao

Beginning Python From Novice to Professional (4) - 演示样本格式字符串

$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'P

Beginning Python From Novice to Professional (9) - Socket

Socket 小型服务器: #!/usr/bin/env python import socket s = socket.socket() host = socket.gethostname() port = 1234 s.bind((host,port)) s.listen(5) while True: c,addr = s.accept() print 'Got connection from',addr c.send('Thank you for connecting') c.close(

Beginning Python From Novice to Professional (7) - 类

类 创建简单类: #!/usr/bin/env python __metaclass__ = type class Person: def setName(self,name): self.name = name def getName(self): return self.name def greet(self): print "Hello,world! I'm %s." % self.name foo = Person() bar = Person() foo.setName('L

Beginning Python From Novice to Professional (4) - 字符串格式示例

$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'P

Beginning Python From Novice to Professional (6) - 函数使用

函数使用 定义函数: #!/usr/bin/env python def hello(name): return 'Hello, ' + name + '' print hello('world') print hello('Gumby') Hello, world Hello, Gumby 斐波那契序列举例: #!/usr/bin/env python def fibs(num): result = [0,1] for i in range(num-2): result.append(resu

Beginning Python From Novice to Professional (5) - 条件与循环

条件与循环 条件执行: name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby' What is your name? Gumby Hello, Mr.Gumby name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby' else: print 'He

Beginning Python From Novice to Professional (1) - 数字和表达式

数字和表达式 加减乘除: >>> 2+2 4 >>> 100-50 50 >>> 3*5 15 >>> 1/2 0 >>> 1.0/2.0 0.5 求余.乘方: >>> 1%2 1 >>> 10%3 1 >>> 2**3 8 >>> -3**2 -9 >>> (-3)**2 9 十六进制.八进制: >>> 0x