如何在Python中获取“printed”文件路径,并在Windows命令行中使用它?

2024-04-20 07:48:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个GUI,它允许您:

  1. 选择一个文件(用户单击第一个按钮,弹出一个文件浏览器,用户可以在其中导航到所需的文件)

    选择后,文件的“文件路径”应为输出

  2. 在Windows命令窗口中执行一个批处理文件(用户将单击第二个按钮,命令窗口将打开并使用步骤1的输出)

    命令窗口中的输入将是{我的文件.bat例如“步骤1中的文件路径”}。


我现在在哪里?你知道吗

我可以输出文件路径,但无法将文件路径输入Windows命令提示符窗口,因为它输入的不是实际的文件路径,而是输出的名称,而不是实际的输出。你知道吗

示例:

output name = x
after step 1, x should be = C\file\path\I\want.plz

按下第二个按钮后:Windows命令提示符打开,窗口中的行写入:MyFile.bat "x"而不是MyFile.bat "C\file\path\I\want.plz"


def Button_Open_File(self):
    import tkinter as tk
    from tkinter import filedialog

    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename()
    file_dir_name = print(**file_path**)

def Button_RUN_BATCH_FILE_ON_FILE_IN_CMD(self):
    import subprocess
    p1 = subprocess.check_call(**r'start cmd /c MyFile.bat "file_path"'**, shell=True, cwd="c:/Users/kimk4/Documents/")
    print('done')

我用**突出显示了我的问题区域,因此您可以看到,当我运行这个程序时,我能够在Oython shell中打印一个文件路径,但是我无法让这个批处理文件工作,因为它没有将实际路径(在窗口中)写入文件,而是直接在Windows命令提示符窗口中写入file_path,而不是c\file\location\that\user\navigates\to.thanks。你知道吗


Tags: 文件path用户nameimport命令路径windows