James
James

Reputation: 274

Looping through a folder of files?

I am working on a secondary project that is scouring the protein data bank for peptides matching specific conditions.

I have a folder containing a large portion of these .pdb files and my game plan is to set this file on the desktop, then use a for-loop to scan through all the files within this folder and bank all necessary data. I am stalled however at the import stage. The file/directory is not being recognized. I am attaching the offending code below:

import os
# - - - - -
#Sector C - Iteration through a folder containing all .pdb files. 
for fi in os.listdir('C:/Users/David/Desktop/pdb_Files'):
    if os.path.isfile(os.path.join('C:/Users/David/Desktop/pdb_Files',  fi)):        
    listatom,R3_coordinates,posg=[],[],[]
    for line in open(fi): # < - - - Issue occurring here.
        ist = line.split()
        id = ist[0]
        if id == 'ATOM':
            typ = ist[2]
            if Peptide1 == 'PRO':
                if typ == 'CA':
                    res,toc,ac=ist[3:6]
                    pos = [float(i) for i in ist[6:9]]
                    if ac >= '0':
                        listatom.append([int(ist[1]),typ,res,toc,ac,np.array(pos)])
                        R3_coordinates.append([int(ist[1]),np.array(pos)]) #List of ALL coordinates.
                    if Plot == True:
                        posg.append(pos)

All help appreciated.

Upvotes: 2

Views: 702

Answers (2)

niemmi
niemmi

Reputation: 17273

You need to provide the full path to open:

path = os.path.join('C:/Users/David/Desktop/pdb_Files',  fi)
if os.path.isfile(path):        
    listatom,R3_coordinates,posg=[],[],[]
    for line in open(path):

Upvotes: 1

gnicholas
gnicholas

Reputation: 2087

This is python. Before you write something to grab all the files in a directory, think if that is a common problem that others have dealt with before.

import glob
print glob.glob("C:/Users/David/Desktop/pdb_Files/*.pdb")
#this returns ["C:/Users/David/Desktop/pdb_Files/file1.pdb", "C:/Users/David/Desktop/pdb_Files/file2.pdb", ...]

The glob module lets you wildcard match on files in a directory, so the above line with return a list of all files in the pdb_Files folder that end with .pdb.

Upvotes: 2

Related Questions