要只写一个字节,请使用
chr(n)获取包含integer的字节
n。
您的代码可以简化为:
import ospath = r'C:UsersmeDesktopoutput'for counter in xrange(100): with open(os.path.join(path,'{:02x}.txt'.format(counter)),'wb') as f: f.write(chr(counter))请注意使用原始字符串作为路径。如果字符串中有’ r’或’ n’,则它们将被视为回车或换行,而无需使用原始字符串。
f.write是写入文件的方法。
chr(counter)生成字节。确保也以二进制模式编写
'wb'。



