首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

[Python]资料的创建与追加

2012-12-18 
[Python]文件的创建与追加一、用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:#python?fo

[Python]文件的创建与追加

一、用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:
#python?
>>>f=open('f.txt','w')??? # r只读,w可写,a追加
>>>for i in range(0,10):f.write(str(i)+'\n')
.? .? .
>>> f.close()

二、文件内容追加,从0到9的10个随机整数:
#python
>>>import random
>>>f=open('f.txt','a')
>>>for i in range(0,10):f.write(str(random.randint(0,9)))
.? .? .
>>>f.write('\n')
>>>f.close()

三、文件内容追加,从0到9的随机整数, 10个数字一行,共10行:
#python
>>> import random
>>> f=open('f.txt','a')
>>> for i in range(0,10):
.? .? .???? for i in range(0,10):f.write(str(random.randint(0,9)))??
.? .? .???? f.write('\n')?????
.? .? .
>>> f.close()

四、把标准输出定向到文件:
#python
>>> import sys
>>> sys.stdout = open("stdout.txt", "w")
>>>? . . .

热点排行