将我的输出(一组文件)定向到在脚本中创建的目录

Direct my output, a set of files, to a directory that was created within the script

我有这个脚本,它输入一个具有类似矩阵格式的文件,特别是序列配置文件 (.pssm) 并提取矩阵的一侧,并将其放入一个新文件中以规范化这些值。目前,该脚本在我工作的目录中创建了一个名为 id.profile 的新文件,并创建了一个名为“norm_seqprof_output”的 EMPTY 文件夹。

我希望它能将该输出放入脚本正在创建的目录中。我尝试了很多不同的东西,但它似乎没有连接正在制作的新目录和输出。


import sys
import os.path
from pathlib import Path


def pssm_list(infile):  # call list of file names and for dsspfile
    ''' Reads relevant lines from a pssm file and saves them to a list.
    Returns values of the 2 matrices (no header).'''
    with open(infile) as ofile:
        flist = ofile.readlines()[3:-6]  # list of each line of the file excluding first 3 & last 6 lines
        return flist


def lines_to_list(infile1):
    ''' Reads all lines from a file and saves them to a list containing the '\n' char. '''
    all_lines_list = []
    with open(infile1, 'r') as rfile:
        all_lines_list = rfile.readlines()
    return all_lines_list  # need to rstrip in a loop for using filenames.


def relevant_lines(infile2):
    '''Takes list (extracted from a .pssm file) and extracts the Sequence Profile Portion only.
    Returns a list of list where each element is one line of the sequence profile matrix. '''
    pssm_profile_list = pssm_list(infile2)  # contains all lines from the pssm file.
    profile_final_list = []  # for holding relevant fileds of the line
    for line in pssm_profile_list:
        pssm_profile_list = line.split()[22:42]  # profile ranges from pos 22-42
        profile_final_list.append(pssm_profile_list)  # appending to final list of lists
    return profile_final_list  # list of lists


# # divide all values by 100
def write_normalized_profile(profile_final_list, ofile):
    '''Takes profile list of lists and outfile name as input. Writes each number that is in
    one of the sublists and devides it by 100. The number is converted to a string and added
    a tab and written to a file. After each sublist a newline character is written to the file.'''
    with open(ofile, "a") as wfile:
        for sublist in profile_final_list:
            #             print(sublist)
            for el in sublist:
                num = int(el) / 100
                numstring = str(num)
                wfile.write(numstring + '\t')  # adding tab after each number
            wfile.write("\n")  # adding newline at the end of each sublist.
            #print(sublist)
            #print(numstring)





if __name__ == '__main__':
    infile = ('/Users/Desktop/PDB/d3ps.pssm') #sys.argv[1]  # the idlist to loop on
    #print(infile)
    # Call the function by looping through an id list+'.pssm' extension
    # name the outfile the same --> id list+'.profile'
    idlist = lines_to_list('/Users/Desktop/PDB/idlist')  # containing the id of the file but NOT the extension ".pssm"
    #print(idlist)
    for ids in idlist:
        #print(ids)
        # path may need to be added before "<the path>"+ids.rsprip()
        part2 = ids.rstrip() + '.pssm'  # removing newlinecharacter, adding necessary extension
        #print(part2)
        if os.path.isfile(infile) == True:  # does this file exist?
            ofile = ids.rstrip() + '.profile'  # outfile for each id with correct extension
            #print(ofile)
            profile_list = relevant_lines(infile)
            #print(profile_list)
            write_normalized_profile(profile_list, ofile)
            #print(write_normalized_profile)
            #print(profile_list)


        else:
            print("Error file: " + infile + " not found.")



        if not os.path.isdir("/Users/Desktop/PDB/norm_seqprof_output"):
            os.mkdir("/Users/Desktop/PDB/norm_seqprof_output")
        with open(ofile, 'r') as G:
            print(G.read())
            sys.stdout = open("file", "w")
            print(G)
            sys.stdout.close

我希望它能将该输出放入脚本正在创建的目录中。

我假设您想将包含 输出 的文件写入目录?

通常您会选择要保存到的目录 ("/Users/Desktop/PDB/norm_seqprof_output"),添加分隔符 "/",然后添加您的文件名 ("id.profile")。

您可以使用 os.path.join 来做到这一点

output_folder = "/Users/Desktop/PDB/norm_seqprof_output"
file_name = "id.profile"
output_file = os.path.join(output_folder, file_name)

或者只是

output_file = output_folder + "/" + file_name 

然后将结果写入output_file