Reputation: 23
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
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