Python assign function to dict value
I am trying to write a script that opens files based on dictionary values. For each key/value it opens a file based on that value's name and assigns that file to the name of the value (I think it's going wrong here). So far it opens the files for me, with the right names, so that works. However, I think the name 开发者_运维知识库I assign the open(file) function to is wrong, since the rest of my function does not open the files anymore, and I can't close them. Example of my script:
filelist=[]
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
for value in codeconv.values():
value=open("c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt", 'w')
filelist.append(value)
"Here I do something with the files"
for f in filelist:
f.close()
So the problem is, how do I assign the open(file) function to the correct name? (Here r1p1d and r2p1d for example) And how do I later call those again?
The error I get now is: AttributeError: 'str'object has no attribute 'close' on the f.close() line.
EDIT: It now works as I want it to, using the following code: (I also included the 'here I do something' part now just for clarity)
result=open("C:\\Biochemistry\\Pythonscripts\\Illuminaresults.txt", "r")
filelist=[]
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
opened_files={}
for key, value in codeconv.items():
filename="c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt"
file=open(filename, 'w')
opened_files.update({key: file})
for line in result:
if line[0]==">":
lastline=line
if line[0:3] in codeconv and len(line)==64:
f=opened_files[line[0:3]]
f.write(lastline+line)
else: continue
for f in opened_files.values():
f.close()
result.close()
I got another problem now though when I try to write the next part of my script, but that's probably something for another question, as it gives a Windowserror not related to this part. Thanks for the help all!
If i understand you well, you want to create some variable dynamically from the dictionary so that you can assign them to the opened files, is that it ???!!!!
I will suggest to do it using another dictionary like this:
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
opened_files = {}
for key, value in codeconv.items():
file_name = "c:\Biochemistry\Pythonscripts\Splittest\split%s.txt" % value
file=open(file_name, 'w')
opened_files.update({key: file})
you can now access your opened files from the dictionary like this:
f = opened_files['agt']
f.read()
....
and for you latter code do it like this:
for f in opened_files.values():
f.close()
I'd caution against reusing "value" in that first loop, for clarity and future bugs that might be introduced there.
But, the code you posted actually works, so obviously there's something else going on that we can't diagnose here. What you need to do is inspect the contents of filelist
before you try to close the file handles in it. That will probably point you towards the answer.
Perhaps you want to append tuples to filelist
so the name is associated with the file object
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
for value in codeconv.values():
f=open("c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt", 'w')
filelist.append((value, f))
"Here I do something with the files"
for name, f in filelist:
f.close()
I can't see a problem in your code, maybe it's not in the part pasted? (in filelist
, or the snipped bit).
But anyway, I would rewrite it to avoid using the same variable, value
, for two different things:
filelist = []
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
for file_id in codeconv.values():
f = open("c:\Biochemistry\Pythonscripts\Splittest\split"+file_id+".txt", 'w')
filelist.append(f)
"Here I do something with the files"
for f in filelist:
if isinstance(f, str):
print "WARNING: Expected file handle, got string:", f
else:
f.close()
(I also added a bit of troubleshooting code)
The files are already open - your filelist
object contains open file objects that you can iterate over (for example, with for line_of_text in filelist[0]:
) or call other functions of (see dir(file)
for other members).
You can defer opening the file by assigning a lambda and calling it later, for example:
for value in codeconv.values():
value2 = lambda: open(complete_filename)
filelist.append(value2)
my_file_object = filelist[0]()
You may prefer to store these in a dictionary:
for value in codeconv.itervalues():
filedict[value] = lambda: open(complete_filename)
my_file_object = filedict["r1p1d"]()
Or if you really want to create new variables (I strongly recommend not doing this, but since you asked):
for value in codeconv.itervalues():
globals()[value] = open(complete_filename)
# or
#globals()[value] = lambda: open(complete_filename)
# if you prefer
Finally, you close the files as you already are (substituting filedict.itervalues()
for filelist
if you use a dictionary instead).
(Obviously you need to replace complete_filename
in the above examples with however you calculate the actual filename. I shouldn't need to say this, but I've been stung too often by leaving out these sorts of details.)
You're not assigning the function, you are assigning the return value. Which appearantly is a string in this case instead of a file object, which is a bit surprising. Are you doing anything with the filelist in the "here I do something" part?
[edit] ok, sounds like you want a dict between key/fileobject (with example usage).
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
filedict = {key, open("c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt", 'w')
for key, value in codeconv.iteritems()}
#Here I do something with the files
filedict["agt"].write("foo")
filedict["aga"].write("bar")
for f in filedict.values():
f.close()
精彩评论