这是我的原代码,从网上搜到要用locale,用locale.getlocale()得到的手机上是('en_US','UTF-8')电脑上是(None,None)
但是两边都不支持用locale. setlocale()修改
感觉这个东西莫名其妙
第1个nowfile=open(nowname,'wb')正常生成了5.txt,但是while里面的nowfile=open(nowname,'wb')明明传入了一样的参数,但是却报了错
#!/usr/bin/python3
import time
import socket
import locale
locale.getlocale()
HOST = '192.168.1.25'
PORT = 47513
nowname='/storage/emulated/0/A_/通讯/5.txt'
print(nowname)
nowfile=open(nowname,'wb')
nowfile.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print('Access!')
odd=b""
while True:
data = s.recv(1024)
if data==b'':
continue
dh=data[:5]
data=data[5:]
if dh[0]==102: #f
if dh[1]==104: #h
nowname='/storage/emulated/0/A_/通讯/'+data[:int.from_bytes(dh[1:3],'big')].decode('utf-8')
print(nowname)
print(dh)
print(data)
nowfile=open(nowname,'wb')
if dh[1]==100: #d
nowfile.write(data[:int.from_bytes(dh[1:3],'big')])
if dh[1]==101: #e
nowfile.close()
if dh[0]==108: #l
print (">> ",data.decode('utf-8'))
"Embedded null byte"错误通常是由于文件名中包含了空字节导致的。在Python中,文件名中如果包含空字节,会导致在打开文件时抛出"Embedded null byte"错误。
为了解决这个问题,您可以尝试以下几种方法:
使用os.path.basename()函数来移除文件名中的空字节。
使用str.replace()函数来移除文件名中的空字节。
使用bytes.decode()函数来移除文件名中的空字节。
使用正则表达式来移除文件名中的空字节。
综上所述,在你的代码中,你可以在获取文件名后使用str.replace()函数来移除文件名中的空字节。
如:
nowname='/storage/emulated/0/A_/通讯/'+data[:int.from_bytes(dh[1:3],'big')].decode('utf-8').replace('\0','')