Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7.10如何使用不同的名称迭代创建文件_Python_Python 2.7 - Fatal编程技术网

Python 2.7.10如何使用不同的名称迭代创建文件

Python 2.7.10如何使用不同的名称迭代创建文件,python,python-2.7,Python,Python 2.7,我想用Python 2.7.10创建不同数量的文件。它们的数量由i索引(已经定义了X的范围(X))决定,名称应该是:file1、file2、file3、file4…filei。我写的是: list3 = range(X) for i in list3: # (here I want to add the creation process) 谢谢大家 我想这个问题是重复的。无论如何,例如,你可以做这样的事情: list3 = range(x) for i in list3: wit

我想用Python 2.7.10创建不同数量的文件。它们的数量由i索引(已经定义了X的范围(X))决定,名称应该是:file1、file2、file3、file4…filei。我写的是:

list3 = range(X)
for i in list3:
   # (here I want to add the creation process)

谢谢大家

我想这个问题是重复的。无论如何,例如,你可以做这样的事情:

list3 = range(x)
for i in list3:
    with open("File%i" % i, "w") as myfile:
        #do something
for x in range(3):
    with open('File' + str(x) +'.txt','w') as f:
        stringa = str(x) + ' Number of file written inside'
        f.write(stringa) 

希望这能解决你的疑问。祝你今天愉快,再见

我想这个问题是重复的。无论如何,例如,你可以做这样的事情:

for x in range(3):
    with open('File' + str(x) +'.txt','w') as f:
        stringa = str(x) + ' Number of file written inside'
        f.write(stringa) 

希望这能解决你的疑问。祝你今天愉快,再见

您可以只打开文件,而不向其写入任何内容来创建文件:

for i in range(x):
    f = open("file{}".format(i), 'w')
    f.close()

您可以只打开文件,而不向其写入任何内容来创建文件:

for i in range(x):
    f = open("file{}".format(i), 'w')
    f.close()

在文件末尾,我建议使用扩展名,特别是如果您使用的是windows OS:-),这将有助于您以后打开文件本身。在文件末尾,我建议使用扩展名,特别是如果您使用的是windows OS:-),这将有助于您以后打开文件本身。再见!这太棒了!你真是个天才!哇!这太棒了!你真是个天才!