我正在尝试获取计算机的主机名,然后将其写入文件。这是我到目前为止所做的,但它不起作用,我哪里出错?
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()
答案 0 :(得分:1)
您好像不是导入所需的模块。此外,你应该尝试&使用with
语句进行文件处理。它更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()