Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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/4/macos/8.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 打开文本文件并在OS X上搜索_Python_Macos_Terminal - Fatal编程技术网

Python 打开文本文件并在OS X上搜索

Python 打开文本文件并在OS X上搜索,python,macos,terminal,Python,Macos,Terminal,我想打开一个文本文件,然后立即搜索某个字符串并突出显示它 我使用: command ="open "+'"'+ file +'"' os.system(command) 但我想要的是: command ="open "+'"'+ file +'"' + "then"+ "ctrl-F(string)" 显然这不起作用,但有办法做到这一点吗?我只希望字符串中的文本以正常的CTRL+F组合键高亮显示。打开后,您就在正确的轨道上了;使用⌘+如果我假设您使用的是OS X上的标记,则可以使用Apple

我想打开一个文本文件,然后立即搜索某个字符串并突出显示它

我使用:

command ="open "+'"'+ file +'"'
os.system(command)
但我想要的是:

command ="open "+'"'+ file +'"' + "then"+ "ctrl-F(string)"

显然这不起作用,但有办法做到这一点吗?我只希望字符串中的文本以正常的CTRL+F组合键高亮显示。

打开后,您就在正确的轨道上了;使用⌘+如果我假设您使用的是OS X上的标记,则可以使用AppleScript通知System Events应用程序执行击键:

os.system("open " + filename)

# You may need to add a sleep() here if the application is not already open and therefore needs time to load
os.system("""osascript -e 'tell application "System Events" to keystroke "f" using {command down}'""")
os.system("""osascript -e 'tell application "System Events" to keystroke \"""" + search_term + "\"'")

你在正确的轨道上与开放;使用⌘+如果我假设您使用的是OS X上的标记,则可以使用AppleScript通知System Events应用程序执行击键:

os.system("open " + filename)

# You may need to add a sleep() here if the application is not already open and therefore needs time to load
os.system("""osascript -e 'tell application "System Events" to keystroke "f" using {command down}'""")
os.system("""osascript -e 'tell application "System Events" to keystroke \"""" + search_term + "\"'")

打开会导致相关应用程序开始运行并加载文件,因此必须以类似按键的方式将ctrl-F+字符串发送到该应用程序。可能会有帮助。这是否可能取决于将在其中打开文件的应用程序是否使用命令行参数,该参数指示应突出显示与给定字符串匹配的字符串。该打开会导致关联的应用程序开始运行并加载文件,因此,ctrl-F+字符串必须以类似按键的方式发送到该应用程序。可能会有帮助。这是否可能取决于打开文件的应用程序是否使用命令行参数,该参数指示应突出显示与给定字符串匹配的字符串。我会在Python级别使用三引号字符串,以避免必须转义这些双引号。@BlacklightShining是–我正在考虑虽然我试图找出哪些反斜杠指向何处,但我希望它简洁明了,避免问题中产生不必要的变量。什么不必要的变量?我只是建议对这两个字符串文本使用三重引号而不是双引号,例如,osascript-e“告诉应用程序系统事件使用{command down}击键f”。我如何在搜索词中执行逻辑和?例如即使猫和狗没有并排出现,它也能匹配猫和狗吗?@user99889否-这只会触发系统查找选项,因此受到应用程序中CMD+F的限制。我会在Python级别使用三引号字符串,以避免必须转义那些双引号。@BlacklightShining是的–我在试图找出哪些反斜杠指向何处时考虑到了这一点,但我希望它简洁明了,避免问题中产生不必要的变量。哪些不必要的变量?我只是建议对这两个字符串文本使用三重引号而不是双引号,例如,osascript-e“告诉应用程序系统事件使用{command down}击键f”。我如何在搜索词中执行逻辑和?例如,即使猫和狗没有并排出现,它也能匹配它们吗?@user99889否-这只会触发系统查找选项,因此受到应用程序中CMD+F的限制。