[英]Different results when running in python interpreter vs. script file
I am trying to use bash functions inside my python script to allow me to locate a specific directory and then grep a given file inside the directory. 我试图在我的python脚本中使用bash函数,以允许我找到特定的目录,然后grep该目录中的给定文件。 The catch is that I only have part of the directory name, so I need to use the bash function find to get the rest of the directory name (names are unique and will only ever return one folder)
问题是我只有一部分目录名,所以我需要使用bash函数find来获取其余的目录名(名称是唯一的,并且只会返回一个文件夹)
The code I have so far is as follows: 到目前为止,我的代码如下:
def get_tag(part_of_foldername):
import subprocess
import os
p1 = subprocess.Popen(["find", "/path/to/directory", "-maxdepth", "1", "-name", "%s.*" % part_of_foldername, "-type", "d"], stdout=subprocess.PIPE)
directory = p1.communicate()[0].strip('\n')
os.chdir(directory)
p2 = subprocess.Popen(["grep", "STUFF_", ".hgtags"], stdout=subprocess.PIPE)
tag = p2.comminucate()[0].strip('\n')
return tag
Here is what's really strange. 这真的很奇怪。 This code works when you enter it line by line into interactive, but not when it's run thru a script.
当您逐行输入交互式代码时,此代码有效,但通过脚本运行时无效。 It also works when you import the script file into interactive and call the function, but not when it's called by the main function.
当您将脚本文件导入到交互式文件中并调用该函数时,它也起作用,但是在主函数调用该脚本文件时,它不起作用。 The traceback I get from running the script straight is as follows:
我从直接运行脚本获得的追溯如下:
Traceback (most recent call last):
File "./integration.py", line 64, in <module>
main()
File "./integration.py", line 48, in main
tag = get_tag(folder)
File "./integration.py", line 9, in get_date
os.chdir(directory)
OSError: [Errno 2] No such file or directory: ''
And it's called in the main function like this: 在主函数中这样调用它:
if block_dict[block][0]=='0':
tag = get_tag(folder)
with "folder" being previously defined as a string. 其中“文件夹”先前已定义为字符串。
Please note we use python 2.6 so I can't use the module check_output unfortunately. 请注意,我们使用的是python 2.6,因此很遗憾,我无法使用模块check_output。
evidently p1.communicate()[0].strip('\\n')
is returning an empty string. 显然
p1.communicate()[0].strip('\\n')
返回一个空字符串。 are you really using the hardcoded value "/path/to/directory"
as in your example? 您是否真的像示例中那样使用硬编码的值
"/path/to/directory"
?
Have you tried using the glob module as opposed to find? 您是否尝试过使用glob模块而不是find方法?
import glob
glob.glob("/path/to/directory/*/SomeDir/path/*")
You can look past multiple dirctories using **: 您可以使用**查看多个目录:
glob.glob("/path/**/SomeDir/path/*")
and that would match /path/to/your/SomeDir/path/file. 它将与/ path / to / your / SomeDir / path / file匹配。
Check the result of p1.communicate()[0]
. 检查
p1.communicate()[0]
。 It maybe empty string. 它可能是空字符串。
.
in "%s.*" % part_of_foldername
seems to be the cause. 在
"%s.*" % part_of_foldername
似乎是原因。
UPDATE UPDATE
Found typo: comminucate
-> comminucate
发现错别字:
comminucate
-> comminucate
def get_tag(part_of_foldername):
p1 = subprocess.Popen(["find", "/path/to/directory", "-maxdepth", "1", "-name", "*%s*" % part_of_foldername, "-type", "d"], stdout=subprocess.PIPE)
out, err = p1.communicate()
directory = out.split('\n')[0]
p1.wait()
if directory:
os.chdir(directory)
p2 = subprocess.Popen(["grep", "STUFF_", ".hgtags"], stdout=subprocess.PIPE)
out, err = p2.communicate()
p2.wait()
return out.rstrip('\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.