我正在尝试获取计算机的主机名,然后将其写入文件。到目前为止,这是我所能获得的,但是它不起作用,我在哪里出错?
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()
似乎您没有导入所需的模块。另外,您应该尝试使用该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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句