【问题标题】:How can I find the output file of a python os script if I don't know the name of it如果我不知道 python os 脚本的名称,如何找到它的输出文件
【发布时间】:2020-06-27 22:17:00
【问题描述】:

我正在编写一个循环来使用 os 包在 python 中生成文件。我的问题是每次生成文件时,我都需要检查文件内容是否满足要求,如果不满足则丢弃。我也不知道文件的名称,因为它由随机生成的名称组成,所以我需要另一种方法在我的代码中引用它。由于我有一个大型数据集,我不想手动执行此操作。

然后我的问题是如何引用刚刚作为 os 命令输出的文件?

如果有帮助,这是我的代码

# each row is for one patient
for row in range(2):
    #for each id, find descriptors
    local_id=df.loc[row,'# localid']
    age=str(df.loc[row,'Age'])
    if df.loc[row,'gender']=='Female':
        gender='F'
    else: gender='M'
    indication=df.loc[row,'indication']
    race=df.loc[row,'race']

    #run loop to run until matching generated patient has matching race and indication
    check=True
    while check:
        if indication=='Breast carcinoma':
            os.system('run_synthea -p 1 -a '+age+' -g '+gender+' -m breast_cancer')
        elif indication=='Hyperlipidemia':
            os.system('run_synthea -p 1 -a '+age+' -g '+gender+' -m veteran_hyperlipidemia')
        elif indication=='Colorectal Cancer / Polyps':
            os.system('run_synthea -p 1 -a '+age+' -g '+gender+' -m colorectal_cancer')
        else: #runs for , not selected, healthy, ovarian cancer, and heart diseases
            os.system('run_synthea -p 1 -a '+age+' -g '+gender)

        # INSET CODE , find output file 
        output_file=''
        has_indication=check_indication(output_file, indication)
        has_race=json.load(output_file)['entry'][0]['resource']['extension'][0]['extension'][1]['valueString']
        if has_indication and has_race==race:
            deID=output_file['entry'][0]['resource']['id']
            thewriter.writerow([local_id,deID])
            check=False

【问题讨论】:

  • 如果文件写入当前工作目录,则新建一个目录,作为工作目录,然后搜索文件。
  • run_synthea 运行时,它的输出是它用数据创建的文件的名称吗?
  • 您需要提供更多关于run_synthea的详细信息。它的目的是什么,它是如何创建该文件的(根据什么规则)?它的输出是什么?您可以控制该应用程序吗?
  • 你知道输出目录吗?
  • 我需要将run_synthea 更改为java -jar synthea-with-dependencies.jar,我还不能让它工作,所以我仍在努力。但是输出的是json格式的合成电子健康记录,其中文件名由随机生成的名称和ID组成,我知道输出目录。那么对于每个循环,我应该尝试引用当时创建的最新文件吗?

标签: python pandas dataframe operating-system


【解决方案1】:

假设您知道新文件所在的目录,您可以在之前和之后拍摄该目录的快照。这很脆弱,因为它假定在命令运行时没有其他任何东西会影响目录。

import os

target_dir = "." # assume current working directory

before_list = set(os.listdir(target_dir))
os.system(...)
new_files = set(os.listdir(target_dir)) - before_list

最好为任务专门创建一个目录

import tempfile
import subprocess as subp
import os

with tempfile.TemporaryDirectory(prefix="synthea_", dir=".") as prog_dir:
    result = qsubp.run(
        'run_synthea -p 1 -a '+age+' -g '+gender+' -m breast_cancer',
        shell=True, cwd=prog_dir)
    out_files = [fn for fn in os.listdir(prog_dir)
        if os.path.isfile(os.path.join(prog_dir, fn))]
    # move files out of temp dir before it is deleted
    for fn in out_files:
        os.rename(os.path.join(prog_dir, fn), fn))

现在out_files 保存了已创建并移动到当前目录的文件列表。

【讨论】:

    猜你喜欢
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2018-06-14
    • 2019-12-08
    • 2010-12-03
    相关资源
    最近更新 更多