小编Dan*_*iel的帖子

Python只在txt文件中写入For语句的最后一位数字

我有2个文件:

text.txt

和:

main.py

main.py,我有以下代码:

for i in range(1,4097):
    i = str(i)
    file = open("text.txt","w")
    file.write(i)
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我想从1生成所有号码- 4096,但是当我运行该脚本,它只是写4096text.txt

我如何在里面写数字 1 - 4096 text.txt

(顺便说一句,我做i了一个字符串 ( str()) 因为它有我和错误说 Python 只能写字符串而不是 Intigers:

Traceback (most recent call last):
  File "d:\path\app.py", line 14, in <module>
    file.write(i)
TypeError: write() argument must be str, not int
Run Code Online (Sandbox Code Playgroud)

)

谢谢

-D一个ne

python string integer for-loop write

0
推荐指数
1
解决办法
49
查看次数

标签 统计

for-loop ×1

integer ×1

python ×1

string ×1

write ×1