sys.setdefaultencoding 引发 [Bad file descriptor]解决

 为了彻底解决python转码的问题,我们可能需要在程序启动的时候加上下面两句:

 reload(sys)
 sys.setdefaultencoding("utf-8")

 但是如果我们以这样的方式来启动:
 python test.py >> test.log
 
 那么会看到这样的错误:
 IOError: [Errno 9] Bad file descriptor: '<fdopen>'

 究其原因 是 reload(sys)导致,因为这个函数会重置 sys.stdout

 【如何解决】
 
 a = sys.stdout
 reload(sys)
 sys.setdefaultencoding("utf-8")
 
 也就是只要我们在reload方法之前引用一下sys.stdout就可以了,很神奇吧,说明python的reload会检查sys下面的变量的引用计数。
发布了24 篇原创文章 · 获赞 5 · 访问量 17万+
展开阅读全文

volley上传图片报read failed: EBADF (Bad file number)

07-12

手机系统android4.4 想上传图片到服务器 基本代码为: ``` public void addPart(final String key, final String fileName, final InputStream fis, String type, final boolean isLast) { writeFirstBoundaryIfNeeds(); try { type = "Content-Type: " + type + "\r\n"; out.write(("Content-Disposition: form-data; name=\"" + key + "\"; filename=\"" + fileName + "\"\r\n").getBytes()); out.write(type.getBytes()); out.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes()); final byte[] tmp = new byte[4096]; int l = 0; //java.io.IOException: read failed: EBADF (Bad file number) // Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number) while ((l = fis.read(tmp)) > 0) { out.write(tmp, 0, l); } if (!isLast) out.write(("\r\n--" + boundary + "\r\n").getBytes()); else { writeLastBoundaryIfNeeds(); } out.flush(); out.close(); fis.close(); } catch (final IOException e) { e.printStackTrace(); } } ``` 报错点位于while ((l = fis.read(tmp)) > 0)这句代码。 完整的报错: 07-12 15:23:29.778: W/System.err(1267): java.io.IOException: read failed: EBADF (Bad file number) 07-12 15:23:29.779: W/System.err(1267): at libcore.io.IoBridge.read(IoBridge.java:435) 07-12 15:23:29.779: W/System.err(1267): at java.io.FileInputStream.read(FileInputStream.java:179) 07-12 15:23:29.780: W/System.err(1267): at java.io.InputStream.read(InputStream.java:162) 07-12 15:23:29.780: W/System.err(1267): at com.yirui.youbao.net.MultipartEntity.addPart(MultipartEntity.java:114) 07-12 15:23:29.781: W/System.err(1267): at com.yirui.youbao.net.MultipartEntity.addPart(MultipartEntity.java:98) 07-12 15:23:29.781: W/System.err(1267): at com.yirui.youbao.net.MultipartRequestParams.getEntity(MultipartRequestParams.java:86) 07-12 15:23:29.782: W/System.err(1267): at com.yirui.youbao.net.MultipartRequest.getBody(MultipartRequest.java:59) 07-12 15:23:29.782: W/System.err(1267): at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:223) 07-12 15:23:29.783: W/System.err(1267): at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210) 07-12 15:23:29.783: W/System.err(1267): at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106) 07-12 15:23:29.783: W/System.err(1267): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93) 07-12 15:23:29.784: W/System.err(1267): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105) 07-12 15:23:29.785: W/System.err(1267): Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number) 07-12 15:23:29.786: W/System.err(1267): at libcore.io.Posix.readBytes(Native Method) 07-12 15:23:29.787: W/System.err(1267): at libcore.io.Posix.read(Posix.java:128) 07-12 15:23:29.788: W/System.err(1267): at libcore.io.BlockGuardOs.read(BlockGuardOs.java:149) 07-12 15:23:29.789: W/System.err(1267): at libcore.io.IoBridge.read(IoBridge.java:425) 07-12 15:23:29.789: W/System.err(1267): ... 11 more 很奇怪,这是什么引起的? 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览