alistair
alistair

Reputation: 23

Write the output of socket module into a file

I'm trying to get the hostname of my computer and then write it to a file. This is what I've got so far, but its not working, where am I going wrong?

def testing():
    os.mkdir("zzzdirectory")
    os.chdir("zzzdirectory")
    fo=open("testfolder.txt", "wb")
    fo.write("this is the first line of the file\n")
    s=socket.gethostname()
    fo.write(s)
    fo.close()

testing()

Upvotes: 2

Views: 150

Answers (1)

shad0w_wa1k3r
shad0w_wa1k3r

Reputation: 13372

It seems you are not importing the required modules. Also, you should try & use the with statement for file handling. It is more pythonic.

import os
import socket

def testing():
    os.mkdir("zzzdirectory")
    os.chdir("zzzdirectory")
    s=socket.gethostname()
    with open("testfolder.txt", "wb") as fo:
        fo.write("this is the first line of the file\n")
        fo.write(s)

testing()

Upvotes: 1

Related Questions