0

I have a py script which creates txt file with some name, but i need that it named it with hostname. for ex when it creates txt file from test1 hostname it gave name to this txt file test1.txt

this is script. how to change it?

def save_passwords(self):
    with open('test.txt','w',encoding='utf-8') as f:
        f.writelines(self.passwordList)
Macattack
  • 1,917
  • 10
  • 15
  • 1
    Does this answer your question? [How can I use Python to get the system hostname?](https://stackoverflow.com/questions/4271740/how-can-i-use-python-to-get-the-system-hostname) – Macattack Feb 16 '21 at 17:22

1 Answers1

0

Your code would look like this:

import socket

def save_passwords(self):     
    hostname = socket.gethostname()
    with open(hostname + '.txt', 'w', encoding='utf-8') as f: 
        f.writelines(self.passwordList)
Josh Hales
  • 394
  • 2
  • 12