我在PyInstaller和Py2exe上遇到了同樣的問題,所以我遇到了來自cx-freeze的FAQ上的解決方案。從控制臺或作為應用程序使用腳本時,以下功能將為您提供“執(zhí)行路徑”,而不是“實際文件路徑”:print(os.getcwd())print(sys.argv[0])print(os.path.dirname(os.path.realpath('__file__')))資料來源:http ://cx-freeze.readthedocs.org/en/latest/faq.html您的舊行(最初的問題):def read(*rnames):return open(os.path.join(os.path.dirname(__file__), *rnames)).read()使用以下代碼段替換您的代碼行。def find_data_file(filename): if getattr(sys, 'frozen', False): # The application is frozen datadir = os.path.dirname(sys.executable) else: # The application is not frozen # Change this bit to match where you store your data files: datadir = os.path.dirname(__file__) return os.path.join(datadir, filename)使用上面的代碼,您可以將應用程序添加到os的路徑中,可以在任何地方執(zhí)行它,而不會出現(xiàn)應用程序無法找到其數(shù)據(jù)/配置文件的問題。用python測試過:3.3.42.7.13
1 回答

holdtom
TA貢獻1805條經(jīng)驗 獲得超10個贊
完全按照您的編碼將Unicode字符串'\xff'轉(zhuǎn)換為UTF-8字節(jié)序列b'\xc3\xbf'。
如果要發(fā)送字節(jié)數(shù)據(jù),請使用字節(jié)字符串(b'\xff\x0a\x82'),然后使用一種PyBytes將其轉(zhuǎn)換為achar*和size的方法:
%typemap(in) (char* data,int size) (Py_ssize_t len) %{
if(PyBytes_AsStringAndSize($input,&$1,&len) == -1)
return NULL;
$2 = (int)len;
%}
請注意,這應該在32位和64位Windows上工作,并且可以將空字節(jié)包含在數(shù)據(jù)中,因為顯式提取了大小,并且可以處理64位的可能性Py_ssize_t。
添加回答
舉報
0/150
提交
取消