1

so what I am trying to do is: 1) Use a for loop to create a folder within a directory, 2) write the output file to this folder

i am trying to use the value in the for-loop as part of the input2 name and the folder name

import os,re
values = ['alpha','123']

query_file = '/Users/name/Desktop/query.txt' # 'a\nb\nc\nd\n4\n5\n6\n'
reading_file = '/Users/name/Desktop/alpha.txt' #'abcdefghijklmnopqrstuvwxyz'
#reading_file = '/Users/name/Desktop/123.txt' #'123456789'

output_file='/Users/name/Desktop/output.txt'

def func(input1,input2,output):
    query=open(input1,'r').read().split('\n')
    reading = open(input2,'r').read()


    dir,file=os.path.split(input1)                
    temp_out= os.path.join(dir,output)            
    out_file=open(temp_out,'w') 

    for line in query:
        m = re.search(line,reading)
        if bool(m) == True:
            out_file.write( str( m.start() ) +'\n')


print func(query_file,reading_file,output_file)

so it creates a file called output.txt that is in the SAME directory as the input but i want it to (create a folder based on the name) and put the file (within the folder)

right now, i have to do this one at a time and create the folders individually to place the files. . . i would like to use the 'values' list as the 'reading_file' endings (the first one ends in 'alpha' followed by .txt extension, and the second one leads to '123' followed by .txt)

in the end there should be two folders:

'/Users/name/Desktop/alpha/output.txt'

'/Users/name/Desktop/123/output.txt'

*note that the outputs should be different because the inputs should change relative to what is being called in the for loop

i apologize if this is confusing but i'm trying to simplify it as much as possible from what my script is doing. let me know if i need to clarify anything

2 Answers 2

3

to make a folder:

import os
os.mkdir(path[, mode])

if you want to make a directory such as

~/dinosaur/llama/nested/etc/

then you should use os.makedirs(path[, mode]) as it builds all the nested paths needed to make sure that is valid


so say you're currently in ~/tmp

>>> import os
>>> os.mkdir('llama')
>>> os.mkdir('me')

to make the folders inside that directory.

but what if we wanted inside ~/llama/me/ but we're still in ~/tmp~? then you can use mkdir twice, or you can just use

>>> import os
>>> os.makedirs('/home/llama/me')
Sign up to request clarification or add additional context in comments.

2 Comments

where do you specify the name of the folder ?
that would be path - it's the path of the folder
2

If I understood your question right you just want to iterate through values and output found locations (something) in special folders (files). So slightly modifying your code

import os, re
values = ['alpha','123']

BASE_PATH = '/Users/name/Desktop/'
OUTPUT_FILE_NAME = 'output.txt'

query_file = os.path.join(BASE_PATH, 'query.txt') # 'a\nb\nc\nd\n4\n5\n6\n'

def func(query_file, reading_files):
    with open(query_file,'r') as f:
        query = f.read().split('\n')

    # TODO: check query

    for fbase in reading_files:
        in_file_name = os.path.join(BASE_PATH, '{}.txt'.format(fbase))
        out_dir = os.path.join(BASE_PATH, fbase)
        out_file_name = os.path.join(out_dir, OUTPUT_FILE_NAME)

        # TODO: check if out_dir exists but is regular file
        if not os.path.isdir(out_dir):
            os.mkdir(out_dir)

        with open(in_file_name, 'r') as in_file, open(out_file_name, 'w') as out_file:
            reading = in_file.read()

            for line in query:
                m = re.search(line, reading)
                if m is not None:
                    out_file.write("{}\n".format(str(m.start())))


print(func(query_file, values))

Basicaly it's easier to work with files with with;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.