Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python(paramiko)-没有这样的文件或目录:u'\u012f\u0161 test.xls';_Python_Paramiko_Ioerror - Fatal编程技术网

Python(paramiko)-没有这样的文件或目录:u'\u012f\u0161 test.xls';

Python(paramiko)-没有这样的文件或目录:u'\u012f\u0161 test.xls';,python,paramiko,ioerror,Python,Paramiko,Ioerror,我的文件名是这个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

我的文件名是这个
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名称的文件从本地目录获取,它也会通过罚款):

p.S.方法参数,如
cr、uid、id
是特定于应用程序的,与远程文件处理无关,因此您可以忽略这些参数

更新 我在日志中注意到:

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.
这发生了,然后我得到了那个错误。是否在使用文件之前会话已关闭

更新2
sftp.listdir()
似乎有问题。当我尝试使用它的文件名时,就像使用标准的
open
,它给出了一个错误,没有这样的文件,因为我猜它只检查本地目录(我不知道它以前是如何工作的…)。如果我尝试使用
sftp.open()
打开,那么它会工作


如何使用远程路径在本地服务器中打开它?

远程文件路径似乎有问题。当我得到一个远程路径并试图直接从中打开时,它将看不到它,并且会抛出一个找不到该文件的错误

所以我重写了我的方法,以稍微不同的方式处理远程文件。现在,它只是下载并在本地打开它,而不是远程打开(并且只有在解析并在本地保存之后)

如果有更好的方法,请随时将其作为答案发布。 下面是我解决问题的新方法:

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         

您确定您在正确的工作目录中吗?文件是否列在
os.listdir(u.')
中?您尝试过指定完整的绝对路径吗?
sys.getfilesystemencoding()
state Python认为文件系统名称使用了什么?@MartijnPieters正如我所说,如果没有unicode字符,它会查找文件。我使用的是完全绝对路径。对,您的错误消息显示了一个相对路径,我只是在这里消除了通常的怀疑。@MartijnPieters奇怪的是,我只使用ascii名称文件进行了检查,它给出了相同的错误,没有找到该文件。但问题是,当通过远程目录时,它实际上会找到该文件并输出其名称。但当打开它时,就会出现错误,说不存在这样的文件。
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