mercredi 16 décembre 2020

Syntax error for 'if... else' statement in Python using .startswith() for reading files and sorting to new directories

I am trying to open files contained within a list, read the files, then sort the files based on what was within the files. I am dealing with two different file types. When I try and run the code I get a syntax error at the location of the elif statement of my code within the function I have defined. Any idea as to why? The line of code is below:

import os 

file_location = os.getcwd() #Gives current directory location
    #Creates lists containing .PDB files
input_pdb = [f for f in os.listdir(file_location) if f.endswith('.pdb')]
    #Creates lists containing .CIF files
input_cif = [ff for ff in os.listdir(file_location) if ff.endswith('.cif')]

print('pdb list', input_pdb)
print('cif list', input_cif)
    # '_pdbx_struct_mod_residue.details' is used to flag .CIF files as having modified residues
    # 'MODRES' is used to flag .PDB files as having modified residues

def read_pdb(file):
    pdb_file = open(file, 'r')
    for text in pdb_file:
        if text.startswith('MODRES') or text.startswith('_pdbx_struct_mod_residue.details'):
            os.rename(file_location, file_location + '/With_MN')
            print(os.rename(file_location, file_location + '/With_MN')
        else: 
            os.rename(file_location, file_location + '/Without_MN')
            print(os.rename(file_location, file_location + '/Without_MN')

The error that I am getting looks like this:

 else:
    ^
SyntaxError: invalid syntax

I am not sure what the issue is. I tried to see if this may be in association with using the .startswith(), but have had no luck.

The print statements everywhere are just for checking the code and will be removed when completed.

Aucun commentaire:

Enregistrer un commentaire