-1

I'm new to Python so apologies if this is a basic mistake, I'm trying to write certain information to a txt file (OS is windows).

I want to write "Platform architecture:" as text, and write the output of platform.architecture to a file. Here is my current code:

import.platform
file = open('C:\\Users\\user\\Desktop\\pc info.txt','w')

file.write('Platform architecture:'),platform.architecture

When I hit run a file named pc info is generated on my desktop as expected, but in the file only Platform architecture: is produced as text. It seems file. write platform.architecture() isn't writing any information to the txt file?

Any ideas please?

5
  • Use with for file handling Commented Mar 19, 2019 at 11:31
  • 1
    How to debug small programs Commented Mar 19, 2019 at 11:38
  • Possible duplicate of Python write string of bytes to file Commented Mar 19, 2019 at 11:40
  • See also SO question here. Commented Mar 19, 2019 at 11:42
  • 2
    The fact that you didn't call the platform.architecture function and that it isn't even inside the parentheses of file.write(...) tells me that you haven't grokked functions yet, and explaining how functions work really isn't what SO is for. It looks to me like you need to spend some time with a programming tutorial. Commented Mar 19, 2019 at 11:45

3 Answers 3

4

Debugging:

SyntaxError: invalid syntax on line 1

import.platform

Should have been:

import platform

You should have called the platform.architecture() while writing to the file.

file.write('Platform architecture:'),platform.architecture

Should have been:

file.write('Platform architectue: {}'.format(platform.architecture()))

Hence:

Using str.format():

import platform
print(platform.architecture())   # ('64bit', 'WindowsPE') (in my case)

logFile = 'Path\\to\\your\\file'    
with open(logFile, 'w') as f:
    f.write('Platform architectue: {}'.format(platform.architecture()))

OUTPUT:

Platform architectue: ('64bit', 'WindowsPE')
Sign up to request clarification or add additional context in comments.

1 Comment

@MrShaun you may edit the question if it is relevant to the current question, otherwise ask a new question. PS. if this answer helped you may accept it: meta.stackexchange.com/questions/5234/… cheers
0
import platform
with open('C:\\Users\\user\\Desktop\\pc_info.txt','w') as file:
    file.write('Platform architecture: {}'.format(platform.architecture()))

1 Comment

Please consider adding an explanation, especially as OP is new to Python.
0
# importing the platform module from the site-packages in python 
import platform 

# adding the file absolute path with name, so you can run the script anywhere
# in you system

file_path =r'C:\Users\user\file.txt'

"""
   creating the file at given path with the write permission,
   using with instead of directly open. (why go here to 
   know -> https://stackoverflow.com/questions/31334061/file-read-using-open-vs-with-open
"""

with open(file_path,'w+') as file:        
    file.write('Platform architecture : {}'.format(platform.architecture()))
    """
       writing the output of platform.architecture() command in the file.
       using `str.format()` function for better readablity.
    """

2 Comments

Please explain why this is the solution?
@Machavity hope the future readers can now find it easy to understand code that i have written

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.