Python从与Python脚本不同的目录调用EXE。

huangapple go评论68阅读模式
英文:

Python Calls EXE from a different directory than the Python Script

问题

我有一个调用EXE的Python脚本,该EXE进而访问并将HTML/CSS/JS文件转换为PDF。重要的是,HTML中的所有CSS和JS引用都是相对路径,因为预期该脚本将在不同计算机上访问。

Python脚本如下所示:

file_directory = os.path.realpath(os.path.dirname(__file__))

PATH_TO_EXE = os.path.join(file_directory, 'html-to-pdf.exe')

def convertHTML(*args):

    if not os.path.isfile(PATH_TO_EXE):
        raise FileNotFoundError("ERROR MESSAGE")

    call_args = f'"{PATH_TO_EXE}" ' + '" "'.join(args) + '"'
    subprocess.call(call_args)

该脚本存储在根目录的子目录中,该根目录是GitHub存储库。

如果我在VSCode中打开存储脚本的子目录并运行此脚本,一切都按预期工作,所有CSS和JS都已加载,因为它们的相对路径位于子目录内。

但是,如果我在VSCode中打开根目录(这对于打开GitHub存储库是正常的)并运行脚本,CSS和JS将无法加载,因为我怀疑它无法找到CSS和JS,因为现在HTML是在根目录中加载的。

我想象中有一种方法可以解决这个HTML问题,肯定可以解决EXE(用C#编写)的问题,以避免这个问题,但我宁愿在Python中处理它,因为这似乎是不一致的地方。

我可以通过命令行从任何目录运行以下命令:

"C:/path/to/exe" "source.html" "output.pdf"

并且它将正常工作,因为HTML是在正确的位置加载的。

英文:

I have a Python script that calls an EXE which in turns accesses and converts HTML/CSS/JS files to PDF. Importantly, all CSS and JS references in the HTML are relative paths as this script is expected to be accessed on different computers.

The Python script looks like this:

file_directory = os.path.realpath(os.path.dirname(__file__))

PATH_TO_EXE = os.path.join(file_directory, 'html-to-pdf.exe')

def convertHTML(*args):

    if not os.path.isfile(PATH_TO_EXE):
        raise FileNotFoundError("ERROR MESSAGE")

    call_args = f'\"{PATH_TO_EXE}\" \"' + '\" \"'.join(args) + '\"'
    subprocess.call(call_args)

The script is stored in a subdirectory of the root directory which is a GitHub repository.

If I run this script in VSCode by opening the subdirectory where the script is stored, everything works as intended, all CSS and JS is loaded since their relative paths are within the subdirectory.

However, if I am to open up the root directory in VSCode (which is normal for opening a GitHub repo) and run the script, the CSS and JS is not loaded because I suspect it is unable to find the CSS and JS because the HTML is now loaded in the root directory.

I imagine there is a way to work this out with the HTML, and certainly with the EXE (written in C#) to avoid this issue but I would rather deal with it in Python since that seems to be the inconsistency.

Can I either force the Python script to run within the same directory that it is stored or ensure it calls the function from the proper location?

I can run in the command line from any directory "C:/path/to/exe" "source.html" "output.pdf" and it WILL work properly since the HTML is loaded in the correct place.

答案1

得分: 0

我解决确保使用正确目录的方案是切换到文件的目录。

代码如下所示:

# 获取文件目录
file_directory = os.path.realpath(os.path.dirname(__file__))

# 获取exe文件的路径
PATH_TO_EXE = os.path.join(file_directory, 'html-to-pdf.exe')

# 如果工作目录与文件目录不同,更改工作目录
if os.getcwd() != file_directory:
    os.chdir(file_directory)

def convertHTML(input, output):
    # 检查exe文件是否存在
    if not os.path.isfile(PATH_TO_EXE):
        raise FileNotFoundError("ERROR")

    # 使用参数调用exe文件
    subprocess.call([PATH_TO_EXE, input, output])

if __name__ == '__main__':
    convertHTML('source.html', 'output.pdf')
英文:

Solution I worked out to ensure the correct directory is being used is to change to the directory of the file.

The code looks as follows:

# get file directory
file_directory = os.path.realpath(os.path.dirname(__file__))

# get path to exe
PATH_TO_EXE = os.path.join(file_directory, 'html-to-pdf.exe')

# change working directory if it is different than the file directory
if os.getcwd() != file_directory:
    os.chdir(file_directory)

def convertHTML(input, output):
    # check if the exe exists
    if not os.path.isfile(PATH_TO_EXE):
        raise FileNotFoundError("ERROR")

    # call the exe with the args
    subprocess.call([PATH_TO_EXE, input, output])


if __name__ == '__main__':
    convertHTML('source.html', 'output.pdf')

huangapple
  • 本文由 发表于 2023年6月22日 02:35:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定