-1

I am trying to use the following line of code to get all the pathnames of files in a given directory:

trainDB = [os.path.abspath(x) for x in os.listdir("C:/Users/sean/Documents/uni/ANLP/Data/Train/DrugBank")]

But the ouput is like so:

['C:\\Users\\sean\\Documents\\uni\\ANLP\\Data\\19-norandrostenedione_ddi.xml', ...]

Whereas I want/expect the resultant path to be:

`'C:\\Users\\sean\\Documents\\uni\\ANLP\\Data\\Train\\DrugBank\\19-norandrostenedione_ddi.xml'

I dont understand why the Train/DrugBank diretories are not in the path, if its any use I am runnig these commands from the ANLP directory which is one above Data

seanysull
  • 720
  • 1
  • 8
  • 24
  • `listdir` only returns the contents of the given directory. You need to `join` that to the given directory, then perhaps pass it to `abspath`. – Peter Wood Apr 03 '18 at 14:05
  • `abspath` is joining the current working directory with the filename from `listdir`. – Peter Wood Apr 03 '18 at 14:06

1 Answers1

0

The following code snippet solved my problem following the advice of @Peter Wood.

pathtrainDB = 'C:\\Users\\sean\\Documents\\uni\\ANLP\\Data\\Train\\DrugBank'
filestrainDB = os.listdir(pathtrainDB)
filepathstrainDB = [os.path.join(pathtrainDB,f) for f in filestrainDB]
seanysull
  • 720
  • 1
  • 8
  • 24