Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3602752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:47:10+00:00 2026-05-18T20:47:10+00:00

I am trying to write a script that opens files based on dictionary values.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:47:10+00:00Added an answer on May 18, 2026 at 8:47 pm

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a script that will download files from an FTP server.
I'm trying to write a script that searches a directory for files and greps
I'm trying to write a perl script that reads filenames in a test.txt file
I'm using ubuntu 11.04 and im trying to write a bash script that opens
I'm trying to write a script that asks for an input file and then
I am trying to write a Python script that will copy files from one
I'm trying to write a VBA script that finds all embedded (.docx) files within
I'm trying to make a script that write text in a file and then
I'm trying to write a script that will create a user in MediaWiki, so
I'm trying to write a script that allows an admin of a photo uploading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.