0

Im trying to create directories and a files within each folder, using data from an input file.

It works for the first one but then gives me the FileExistsError

I have been staring at this for hours now and just can't seem to get it, any help would be appreciated.

File data looks like this

>unique id
string of unknown length

and the code I've tried is this

import os


# find a character

CharLocArray = []

NewLineArray = []

with open('/home/tjbutler/software/I-TASSER5.0/seqdata/Egg_protein/seq.fasta', 'r') as myfile:

    data = myfile.read()
    GreaterThan = '>'
    NewLine = '\n'

    # code to read char into var
    # myfile.read().index('>')
    index = 0
    while index < len(data):
        index = data.find('>', index)
        CharLocArray.append(index)
        if index == -1:
            break

        index += 2

    index2 = 0
    while index2 < len(data):
        index2 = data.find('\n', index2)
        NewLineArray.append(index2)
        if index2 == -1:
            break

        index2 += 2

    i = 0
    print(len(CharLocArray))

    while i < len(CharLocArray):
        print(i)
        CurStr = data[CharLocArray[i]:]
        CurFolder = CurStr[CharLocArray[i]:NewLineArray[i]]
        print(CurFolder)
        CurData = CurStr[CharLocArray[i]:CharLocArray[i + 1]]
        print(CurData)
        newpath = r'/home/tjbutler/software/I-TASSER5.0/seqdata/Egg_protein/'
        DirLocation = newpath + CurFolder
        print(DirLocation)
        FileLocation = DirLocation + '/seq.fasta'
        print(FileLocation)
        i = i + 1
        print(i)
        if not os.makedirs(DirLocation):
            os.makedirs(DirLocation)
            file = open(FileLocation, 'w+')
            file.write(CurData)
            file.close()
0

1 Answer 1

3

os.makedirs() should not be used that way - use its exist_ok argument instead:

    os.makedirs(DirLocation, exist_ok=True)  # instead of the condition!
    with open(FileLocation, 'w+') as f:
        f.write(CurData)

also, don't manually create your own paths (i.e. FileLocation = DirLocation + '/seq.fasta'), use os.path facilities instead, e.g.: FileLocation = os.path.join(DirLocation, seq.fasta).

Sign up to request clarification or add additional context in comments.

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.