-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?

Stormvirux
  • 879
  • 3
  • 17
  • 34
MrShaun
  • 43
  • 1
  • 5
  • Use `with` for file handling – DirtyBit Mar 19 '19 at 11:31
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Patrick Artner Mar 19 '19 at 11:38
  • Possible duplicate of [Python write string of bytes to file](https://stackoverflow.com/questions/17349918/python-write-string-of-bytes-to-file) – ZF007 Mar 19 '19 at 11:40
  • See also SO question [here](https://stackoverflow.com/q/48003241/8928024). – ZF007 Mar 19 '19 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. – Aran-Fey Mar 19 '19 at 11:45

3 Answers3

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')
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • @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: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work cheers – DirtyBit Mar 19 '19 at 12:45
0
import platform
with open('C:\\Users\\user\\Desktop\\pc_info.txt','w') as file:
    file.write('Platform architecture: {}'.format(platform.architecture()))
Zak Stucke
  • 432
  • 6
  • 18
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.
    """
sahasrara62
  • 10,069
  • 3
  • 29
  • 44