How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)
问题描述
因此,我尝试对某个任意文件调用Windows命令类型。不幸的是,每当我将cmd从外壳命令转换为非外壳命令时,都会失败。因此,我不能使用推荐的方法来确保我的Python脚本不会被利用。这里有一个例子。
import subprocess
cmd = "type" + '"' + "some_file_with_no_spaces_or_other_things_wrong" + '"'
p = subprocess.pOpen(cmd, shell = True)
但当我尝试时:
#Assume cmd split is done properly. Even when I manually put in the 
#array with properly escaped quotes it does not work
subprocess.pOpen(cmd.split(), shell = False)
失败了,我不知道如何解决这个问题。我希望能够通过将外壳设置为FALSE来安全地调用此命令,但无论何时这样做,我都会收到以下错误。
Traceback (most recent call last):
  File "C:UsersSkyliongitLVDOWinBinOsiris.py", line 72, in openFileDialog
    stderr = STDOUT, shell = False, bufsize = 0, universal_newlines=True)
  File "C:Python34libsubprocess.py", line 859, in __init__
    restore_signals, start_new_session)
  File "C:Python34libsubprocess.py", line 1112, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
subprocess.Popen(['type'])
将抛出错误。我的问题是如何清理文件名,以便可以运行带有SHELL=True的文件名,或者使SHELL=FALSE正常工作。
如有任何有关如何以这种方式正确打开文件的帮助,我们将不胜感激。
推荐答案
type是内部命令,因此您需要运行cmd.exe,例如,隐式通过shell=True运行。
subprocess.list2cmdline()将列表转换为字符串以传递给CreateProcess()Windows API。其语法与cmd.exe语法不同。详情请参见read the links in this answer。
将外壳命令作为字符串传递并添加shell=True:
from subprocess import check_call
check_call(r'type "C:pathwith spaces & special symbols.txt"', shell=True)
注意:r''前缀用于避免转义文字字符串中的后继。
如果该命令在命令行中按原样工作,则它也应该在Python中工作。
如果文件名是在变量中给定的,则可以使用^将其转义为外壳cmd:
escaped_filename = filename_with_possible_shell_meta_chars.replace("", "^")[:-1]
check_call('type ' + escaped_filename, shell=True)
注意:没有明确的引号。
显然,您可以在纯Python中模拟type命令:
向控制台设备(或其他位置)键入副本 如果重定向)。不检查文件是否为可读文本。
如果您只需要读取文件;请使用open()函数:
with open(r'C:pathwith spaces & special symbols.txt', 
          encoding=character_encoding) as file:
    text = file.read()
如果不指定显式编码,则open()使用'cp1252'(locale.getpreferredencoding(False))等ANSI代码页将文件内容解码为Unicode文本。
注意:您必须考虑此处的4个字符编码:
- 文本文件本身的字符编码。它可以是任何内容,例如utf-8
- 图形用户界面应用程序使用的ANSI代码页,例如cp1252或cp1251
- cmd.exe使用的OEM代码页,例如cp437或cp866。它们可用于type命令重定向时的输出
- utf-16由Unicode API使用,如- WriteConsoleW(),例如使用- cmd /U开关时。注意:Windows控制台显示的是UCS-2,即仅支持BMP Unicode字符,但复制-粘贴功能即使对于😊 (U+1F60A)这样的星形字符也有效。
请参阅Keep your eye on the code page。
要将Unicode打印到Windows控制台,请参阅What's the deal with Python 3.4, Unicode, different languages and Windows?
这篇关于如何在Python中安全地调用随机文件上的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在Python中安全地调用随机文件上的类型?
 
				
         
 
            
        基础教程推荐
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				