Martijn Nosyncerror
Martijn Nosyncerror

Reputation: 192

Using a string, to give a name to a new string

What I'm trying to do:

I have some files in a directory, and I made a list of these:

filesInDir = os.listdir("scanfiles")

And after I got these, I'm trying to split the lines into seperate lists:

for files in filesInDir:
    sourceFile = open("scanfiles/" + files, "r")
    dynmicNameList = sourceFile.read().splitlines()

I would like it so that the array name is the file's name. So far I've only seen way more complicated scenarios for this problem. But I can't get this working.

Upvotes: 0

Views: 68

Answers (2)

tschiela
tschiela

Reputation: 5271

I would use an associative Array

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1122162

You want a dictionary for those lines, not local variables:

lines = {}

for files in filesInDir:
    sourceFile = open("scanfiles/" + files, "r")
    lines[files] = sourceFile.read().splitlines()

Upvotes: 3

Related Questions