我的文件名为file_path=u'įš-test.xls
。
当我尝试这个时:
from xlrd import open_workbook
wb = open_workbook(file_path)
我收到此错误:
File "/usr/local/lib/python2.7/dist-packages/xlrd/__init__.py", line 394, in open_workbook
f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: u'\u012f\u0161-test.xls'
如果我改为:
wb = open_workbook(file_path.encode('utf-8'))
IOError: [Errno 2] No such file or directory: '\xc4\xaf\xc5\xa1-test.xls'
注意
paramiko
可能存在问题,因为文件是从远程目录中获取的,使用此方法(如果带有unicode名称的文件来自本地目录,它也可以正常传递):
from contextlib import closing
import socket, os
from paramiko import SSHConfig, SSHClient, AutoAddPolicy, AuthenticationException
def check_remote_dir(self, cr, uid, ids, direct, arch_dir, gec_type, fmt, context=None):
rec = self.browse(cr, uid, ids, context=context)[0]
with closing(SSHClient()) as ssh:
ssh.set_missing_host_key_policy(AutoAddPolicy())
try:
ssh.connect(rec.host, username=rec.user, password=rec.password)
except socket.gaierror:
raise orm.except_orm(_('Error!'),
_("Name or service '%s' not known") % (rec.host))
except AuthenticationException:
raise orm.except_orm(_('Authentication Fail!'),
_("Bad username or password"))
with closing(ssh.open_sftp()) as sftp:
try:
sftp.chdir(direct)
except IOError:
raise orm.except_orm(_('Error!'),
_('Remote directory %s not found') % (direct))
try:
os.chdir(arch_dir)
except OSError:
raise orm.except_orm(_('Error!'),
_('Archive directory %s not found') % (arch_dir))
gec_obj = self.pool.get('card.gec.data')
for f in sftp.listdir():
for fmt in fmt.replace(' ', '').split(','): #removing any whitespace and then splitting in list
length = len(fmt) + 1
if f[-length:] == ".%s" % (fmt):
gec_id = gec_obj.create(cr, uid, {'name': f, 'gec_type': gec_type})
self._resolve_parse(cr, uid, gec_id, gec_obj, gec_type, f, context=context)
self.archive_file(f, None, add_dt=True, remote=True)
sftp.remove(f)
break #only need to check till first occurence
P.S。方法参数如cr, uid, ids
是特定于应用程序的,并且与远程文件处理无关,因此您可以忽略这些
更新 我在日志中注意到了这一点:
2015-02-03 10:23:48,143 10430 INFO amb_test paramiko.transport.sftp: [chan 1] Opened sftp connection (server version 3)
is-test.xls
2015-02-03 10:23:48,162 10430 INFO amb_test paramiko.transport.sftp: [chan 1] sftp session closed.
发生这种情况,然后我收到了这个错误。是否可以在使用文件之前关闭会话?
UPDATE2
sftp.listdir()
似乎有问题。当我尝试使用它的文件名时,就像使用标准的open
一样,它给出了错误,没有这样的文件,我猜它只检查本地目录(我不知道它以前是如何工作的。 )。如果我尝试使用sftp.open()
打开,那么它可以正常工作。
我如何使用远程路径在本地服务器中打开它?
答案 0 :(得分:0)
远程文件路径似乎有问题。当我有一个远程路径并尝试直接从它打开时,它不会看到它并会抛出一个错误,找不到这样的文件。
所以我重写了我的方法,以稍微不同的方式处理远程文件。现在它只是下载并在本地打开它,而不是远程打开(并且只有在解析后才能在本地保存)。
如果有更好的方法,请随时将它们作为答案发布。 所以这里我的新方法解决了我的问题:
def check_remote_dir(self, cr, uid, ids, rem_dir, local_dir, arch_dir, gec_type, fmt, context=None):
"""
Only used to check remote directories and download files
locally
"""
rec = self.browse(cr, uid, ids, context=context)[0]
with closing(SSHClient()) as ssh:
ssh.set_missing_host_key_policy(AutoAddPolicy())
try:
ssh.connect(rec.host, username=rec.user, password=rec.password)
except socket.gaierror:
raise orm.except_orm(_('Error!'),
_("Name or service '%s' not known") % (rec.host))
except AuthenticationException:
raise orm.except_orm(_('Authentication Fail!'),
_("Bad username or password"))
with closing(ssh.open_sftp()) as sftp:
try:
sftp.chdir(rem_dir)
except IOError:
raise orm.except_orm(_('Error!'),
_('Remote directory %s not found') % (rem_dir))
try:
os.chdir(local_dir)
except OSError:
raise orm.except_orm(_('Error!'),
_('Archive directory %s not found') % (arch_dir))
gec_obj = self.pool.get('card.gec.data')
for f in sftp.listdir():
sftp.get(f, f)
sftp.remove(f)
#handle files locally
rec.check_dir(local_dir, arch_dir, gec_type, fmt)
def check_dir(self, cr, uid, ids, direct, arch_dir, gec_type, formats, context=None):
rec = self.browse(cr, uid, ids, context=context)[0]
try:
os.chdir(direct)
except OSError:
raise orm.except_orm(_('Error!'),
_('Directory %s not found') % (direct))
directs = os.listdir(direct)
gec_obj = self.pool.get('card.gec.data')
formats = formats.replace(' ', '').split(',') # Removing any whitespace and then splitting in list
for f in directs:
for fmt in formats:
length = len(fmt) + 1
if f[-length:] == ".%s" % (fmt):
gec_id = gec_obj.create(cr, uid, {'name': f, 'gec_type': gec_type})
self._resolve_parse(cr, uid, gec_id, gec_obj, gec_type, f, context=context)
self.archive_file(f, arch_dir, add_dt=True)
break #only need to check till first occurence