简体   繁体   English

在python解释器和脚本文件中运行时的结果不同

[英]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.

相关问题 Python os和gob:在单个文件与多个文件上执行脚本时,结果不同 - Python os and gob: Different results when executing a script on a single file vs. multiple files Python textwrap:终端打印与写入文件的不同结果 - Python textwrap: different results for terminal print vs. write to file Python解释器和脚本输出不同的结果 - Python interpreter and script outputs different results 检测Python代码的运行位置(例如,在Spyder解释器与IDLE与cmd之间) - Detect where Python code is running (e.g., in Spyder interpreter vs. IDLE vs. cmd) 从 Python 解释器运行脚本文件 - Running script file from Python interpreter Python:运行脚本但未在解释器中时出现TypeError - Python: TypeError when running script but not in interpreter Python-在Shell中运行与在另一个脚本中运行时对路径的解释不同吗? - Python - Interpreting paths differently when running in shell vs. when running within another script? 在python sklearn与R bnlearn中运行朴素贝叶斯的不同结果 - Different results running naive Bayes in Python sklearn vs. R bnlearn 运行python cgi脚本解释器结果与浏览器不同 - Running python cgi script Interpreter results differ to browser Python中的Apache Avro性能非常慢,编码到消息与文件时的结果不同 - Brutally slow Apache Avro performance in Python, different results when encoding to messages vs. files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM