Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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在每个子文件夹中创建一个文件_Python - Fatal编程技术网

python在每个子文件夹中创建一个文件

python在每个子文件夹中创建一个文件,python,Python,我需要帮助! 我需要创建10个子目录:A1,A2,…,A10。每个子目录都包含一个名为position的文件,其中包含以下内容: 2.22222 0.44444 1.58810 5.77778 1.5556 0.41190 每个文件的结尾都包含一个对应于子目录的位置: 例如: 目录A1中的位置: 2.22222 0.44444 1.58810 5.77778 1.55556 0.41190 1.00000 1.00000 1.00000 目录A2中的位置包含 2.2222

我需要帮助! 我需要创建10个子目录:A1,A2,…,A10。每个子目录都包含一个名为position的文件,其中包含以下内容:

2.22222  0.44444  1.58810
5.77778  1.5556  0.41190
每个文件的结尾都包含一个对应于子目录的位置: 例如: 目录A1中的位置:

2.22222  0.44444  1.58810
5.77778  1.55556  0.41190
1.00000  1.00000  1.00000
目录A2中的位置包含

2.22222  0.44444  1.58810
5.77778  1.55556  0.41190
2.00000  2.00000  2.00000
类似于目录A3中的位置,包含:

2.22222  0.44444  1.58810
5.77778  1.55556  0.41190
3.00000  3.00000  3.00000
等等。我试图用python编写代码,但当出现错误时,它并没有很好地工作 IOError:[Errno 21]是一个目录:

import os
content += u"0.22222  0.44444  0.58810"
content += u"0.77778  0.55556  0.41190" 
num = 0
for i in range(0, 10)
   num = num + 1
   subdirectory = str("A")+str(num)
   os.mkdir (subdirectory)
   filename = str(position)
   for ip in open(filename):
   with open (os.path.join(subdirectory)) as ip:
   fp.write(content)
   for ip in open(filename):
      with open (os.path.join(subdirectory)) as ip:
          ft.write"{:.5f}  {:.5f}  {:.5f} ".format(num, num, num)

请帮我调试这个代码

您的第一个for循环需要在“for i in range(0,10)”位后加一个冒号

可能成为

for i in range (0, 10):
    num += 1
我想我能看到的唯一错误就是缺少冒号

num += 1 

做与您所做的完全相同的事情,这是一种很好的快速编码方式。你的缩进也不正确,所以如果你把它也整理出来,应该没问题。

它在我的机器上工作

import os
dir_path = os.path.dirname(os.path.realpath(__file__))
# print(dir_path)     #here, you will choose the working directory. currently, it is the same as your python file.
# for readibility, you can use multi-line string here.
content = '''
2.22222  0.44444  1.58810
5.77778  1.5556  0.41190
'''
for i in range(0, 10):
   foldername = dir_path+"\\"+'a'+str(i+1)
   os.makedirs(foldername)
   # use encoding 'utf-8' if you would like so.
   with open(foldername+"\\position.txt",'w',encoding='utf-8') as 
       innerfile:
       innerfile.write(content)
       innerfile.write('{:.5f}  {:.5f}  {:.5f}'.format(i+1,i+1,i+1))

pathlib可以更容易地考虑这一点,因为它可以将自己的路径名连接起来。如果目录不存在,此代码剪切将生成目录,并创建一个文件(如果不存在),并将
2.000002.000002.00000…
字符串附加到每个目录中

from pathlib import Path
dirnames = {'A01': 1, 'A02': 2, 'A03': 3, 'A10': 10}
basePath = Path('./foobar') # base path to work in
myFile = Path('file.txt') # name of each file to append to
repeats = 4 # number of times to repeat the appended value string
for name, value in dirnames.items():
    # Create the file    
    myName = Path(basePath/name)

    # Make the directories
    try:
        myName.mkdir()
    except FileExistsError:
        print('{} exists, skipping'.format(myName))

    # make the string that will be appended to each file
    myString = ''
    print(myFile)
    for i in range(0, repeats):
        myString = myString + ('{:.5f} '.format(value))
    myString + '\n'

    fileName = Path(myName/myFile) # set the file name 

    # write the string out to the file
    with fileName.open(mode='a') as dataOut:
        dataOut.write(myString)

你能修复缩进吗?还有,很多变量都没有实例化!
from pathlib import Path
dirnames = {'A01': 1, 'A02': 2, 'A03': 3, 'A10': 10}
basePath = Path('./foobar') # base path to work in
myFile = Path('file.txt') # name of each file to append to
repeats = 4 # number of times to repeat the appended value string
for name, value in dirnames.items():
    # Create the file    
    myName = Path(basePath/name)

    # Make the directories
    try:
        myName.mkdir()
    except FileExistsError:
        print('{} exists, skipping'.format(myName))

    # make the string that will be appended to each file
    myString = ''
    print(myFile)
    for i in range(0, repeats):
        myString = myString + ('{:.5f} '.format(value))
    myString + '\n'

    fileName = Path(myName/myFile) # set the file name 

    # write the string out to the file
    with fileName.open(mode='a') as dataOut:
        dataOut.write(myString)