Python之I/O

本文详细介绍了如何使用Python生成列表并将其写入文件,同时提供了读取文件内容的方法。此外,还涵盖了文件操作的基本知识,包括写入、读取、模式选择等,并提供了示例代码帮助理解。
my_list = [i**2 for i in range(1,11)]
# Generates a list of squares of the numbers 1 - 10

f = open("output.txt", "w") ##Open output.txt in w mode

for item in my_list:
    f.write(str(item) + "\n")

f.close()

###You can open files in write-only mode ("w"), read-only mode ("r"), read and write mode ("r+"), and append mode ("a", which adds any new data you write to the file to the end of the file).

read
除了像上面说的,可以打开一个文件,往里面写入需要的内容,还可以读取文件,读取文件的命令:

print f.read()

my_file = open ("output.txt","r")
print my_file.read ()
my_file.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值