1

So I need to create a script and save the info into a text file, and here is what I did:

import platform

def bios_version():
    uname_result = platform.uname()
    pl = {}
    pl['BIOS version'] = uname_result.version

    for a, b in pl.items():
        print(a, ":", b)

#The def is working perfectly but I don't know how to save the file.

print(bios_version())
save_path = 'D:/'
name_of_file = input("What is the name of the file: ")
completeName = os.path.join(save_path, name_of_file+".txt")

f = open(completeName, "w")
f.write(bios_version())
f.close

So it return:

f.write(bios_version())
TypeError: write() argument must be str, not None"

2 Answers 2

1

Try below code:

  • I don't understand why you are using loop and why to store that version detail in dictionary. If there is only single value then just store that in variable and directly return the value and just write into file.

Code

import platform
import os

def bios_version():
    uname_result = platform.uname()
    pl = f"BIOS version: {uname_result.version}"
    return pl

save_path = 'D:/'
name_of_file = input("What is the name of the file: ")
completeName = os.path.join(save_path, name_of_file+".txt")

f = open(completeName, "w")
f.write(bios_version())
f.close

Output:

BIOS version: <<Your version detail>>
Sign up to request clarification or add additional context in comments.

7 Comments

HI, thank you for the suggestion. Im a newbie in python so there are a lot of things i still confused with ;)
Is my solution, make sense to you or not @KorDWLO
Good catch about the single value. But not so good if he needs more keys in the future.
Then he will change the code to loop, but for now this is only the best way. At this point he must need to understand when to use which data type and all the stuff.
hi Jain, the suggestion abut def is good but it is not saving the content of the def
|
0

Here's an alternative, considering other answers:

import platform
import os

def write_bios_version(file_name = None):
    pl = {}
    pl['BIOS version'] = platform.uname().version

    with open(file_name, 'w') as f:
        for a, b in pl.items():
            print(a, ":", b, file=f)

save_path = 'D:/'
name_of_file = input("What is the name of the file: ")
complete_name = os.path.join(save_path, name_of_file+".txt")
write_bios_version(complete_name)

12 Comments

For the def, it works perfectly fine for me. I just have trouble with saving the screen.
write() argument must be str, not None". When you don't include a return statement in a function, by default it returns None. You have to change the function code.
And saying that you want to "save the screen" is a bad term. You actually want to "save" your output (print statements), and if you return the pl variable from your function write will work.
print(bios_version()) also prints nothing since you're not returning anything from the function.
Hi, so i change it as u suggest and it tell me this "f.write(bios_version()) TypeError: write() argument must be str, not dict"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.