首页 > 系统相关 >windows上使用python2.7获取svn info,中文路径乱码问题

windows上使用python2.7获取svn info,中文路径乱码问题

时间:2024-01-30 16:24:14浏览次数:24  
标签:info svn getfilesystemencoding subprocess 乱码 command __ print sys

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import subprocess
import cmd
import os

os.environ['LANG'] = 'en_US.UTF-8'

class SVNCommand(cmd.Cmd):
    def do_svninfo(self, folder_path):
        # 构建svn info命令
        command = ['svn', 'info', folder_path]
        print(command)
        try:
            # 转换命令字符串为本地编码
            if isinstance(command, list):
                command = [c.encode(sys.getfilesystemencoding()) for c in command]
            else:
                command = command.encode(sys.getfilesystemencoding())
            
            # 运行svn info命令并捕获输出
            process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            
            output, error = process.communicate()

            if error:
                print("Error executing SVN command:")
                # print(error.decode(sys.getfilesystemencoding())) # 在这里改成.decode(sys.getfilesystemencoding())
                print(error)
                return

            print(output.decode('utf-8'))

        except subprocess.CalledProcessError as e:
            # 如果命令运行错误,则将错误信息打印出来
            print("Error executing SVN command:")
            print(e.output.decode('utf-8'))

    def default(self, line):
        print("Unknown command:", line)

if __name__ == '__main__':
    folder_path = u"F:\\workDoc\\Doc\\我的文档"
    SVNCommand().do_svninfo(folder_path)

encode方法用于将str类型的字符串转换为bytes类型的二进制数据,这个过程也称为“编码”。

decode方法用于将bytes类型的二进制数据转换为str类型的字符串,这个过程也称为“解码”。

其中最重要就是使用sys.getfilesystemencoding ()函数获取文件系统使用的编码方式,这个函数会根据操作系统的不同返回不同的值

# 转换命令字符串为本地编码
if isinstance(command, list):
command = [c.encode(sys.getfilesystemencoding()) for c in command]
else:
command = command.encode(sys.getfilesystemencoding())

 

标签:info,svn,getfilesystemencoding,subprocess,乱码,command,__,print,sys
From: https://www.cnblogs.com/Cxiangyang/p/17997362

相关文章

  • git客户端中文乱码,解决方案
    git命令如下所示: 解决方案gitconfigcore.quotepathfalse 解决后: 参考:修复gitdiff正文中文乱码-简书(jianshu.com)git中文名转义带来的麻烦;git配置之core.quotepath;git中文乱码-yaowenxu-博客园(cnblogs.com)......
  • DevExpress WinForms中文教程 - 如何创建可访问的WinForms应用?(一)
    为用户创建易访问的WindowsForms应用程序不仅是最佳实践的体现,还是对包容性和以用户为中心的设计承诺。在应用程序开发生命周期的早期考虑与可访问性相关的需求可以节省长期运行的时间(因为它将决定设计决策和代码实现)。一个可访问的WinForms应用程序提供了各种好处,包括:扩大......
  • requests响应文本乱码解决办法
    1.请求百度首页,响应文本页面标题乱码乱码原因:requests获取响应文本之前,会有一个解码的过程,解码就有编码格式,编码格式在响应头content-type里获取,未获取到或者未获取成功,会随便使用默认的编码格式,可能会造成乱码2.查看原本的编码格式图片上运行结果显示原本的编码格式未获取......
  • .Net Core报“‘GB2312‘ is not a supported encoding name. For information on def
    1、......
  • Winform仅允许运行一个程序,当要打开多个时将显示已在运行的程序
    要实现此功能,只需在Program类中修改启动程序的方法即可首先引用对应的命名空间usingSystem.Diagnostics;usingSystem.Reflection;usingSystem.Runtime.InteropServices;接着在类中,Main方法外写///<summary>///Themainentrypointfortheapplication./......
  • GET&POST请求和响应的中文乱码解决方案
    Serlvet程序的请求和响应乱码问题get请求与post请求数据乱码publicclassRequestAPIServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{//获取请求......
  • Error: unable to perform an operation on node 'rabbit@pro'. Please see diagnosti
    简短的和全限定RabbitMQ节点名称rabbitmq支持简短的和全限定域名作为节点名称,但是默认的是简短的,我这里使用了全限定的域名,所以在集群操作stop_app的时候报错了  在rabbitmq安装目录下的/etc/rabbitmq加上配置文件rabbitmq-env.conf(环境变量)就可以了#开启使用全限定节点名......
  • NSIS打包教程 Winform程序打包
    NSIS软件下载地址:https://pan.baidu.com/s/1sbBX__7Q4ntHeEDp-yl1ng提取码:xeax相关的视频教程https://www.bilibili.com/video/BV1jf4y1a7ji?share_source=copy_web(如果不会请看我录制的视频教程)NSIS打包教程Wnform程序打包,,安装部署步骤如下图 桌面快......
  • Blazor SSR/WASM IDS/OIDC 单点登录授权实例5 - Winform 端授权
    目录:OpenID与OAuth2基础知识BlazorwasmGoogle登录BlazorwasmGitee码云登录BlazorSSR/WASMIDS/OIDC单点登录授权实例1-建立和配置IDS身份验证服务BlazorSSR/WASMIDS/OIDC单点登录授权实例2-登录信息组件wasmBlazorSSR/WASMIDS/OIDC单点登录授权实例3-服......
  • 解决SVN文件不显示绿色小钩图标问题
    1相关知识1.1SVN基础SVN是Subversion的缩写,是一个开放源代码的版本控制系统。这个系统主要管理随着时间而改变的数据,这些数据被保存在一个中央资料档案库(repository)中,就像一个普通的文件服务器,但不同的是它会记录每一次文件的变动。这个系统主要用于多个人共同开发同一个项目,实现......