简体   繁体   English

如何在Python代码中修改文件路径,使其在其他计算机上工作?

[英]How to modify a filepath in Python code so that it works on other computers?

I am writing a simple code that runs a batch file and opening of an excel sheet to store some data. 我正在编写一个简单的代码,该代码运行批处理文件并打开excel工作表以存储一些数据。 I have given the location of the file on my computer. 我已经在计算机上指定了文件的位置。 I want to use "pyinstaller" to create an executable so that I can run in other computers as well. 我想使用“ pyinstaller”创建可执行文件,以便我也可以在其他计算机上运行。 Where should I put the *.xlsx and *.bat file in the new computer so that it works, and also should I change the file path in my program? 我应该将* .xlsx和* .bat文件放在新计算机的什么位置,以便它可以工作,还应该在程序中更改文件路径? If so how? 如果可以,怎么办?

filepath = r"F:\project\printingdatabase.xlsx"
wb = load_workbook(filepath)

subprocess.call(r"F:\project\batfile\tester.bat")
# This gets you the dir of the current script in an os-independent way, for example
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 

# This appends a subdir "sub-dir" to the base path, in an os-independent way
os.path.join(BASE_DIR, 'sub-dir')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中引用一个目录,使其在多台计算机上工作? - How do I reference a directory in Python so it works on multiple computers? 我应该如何修改我的代码以便 twosum 函数工作? - How should I modify my code so that twosum function works? Python:如何修复此代码,使其可在Windows上使用? - Python: How can I fix this code so it works on Windows? 如何修复此数字排序python代码,以便其正常工作 - how to fix this number sorting python code so that it works correctly 如何在没有python的情况下将我的Python脚本分发到其他计算机? - How to distribute my Python script to other computers without python? 如何修改数据框以便在 Python Pandas 列中的其他字符之间取值? - How to modify Data Frame so as to take values between some other character in column in Python Pandas? 如何使 python 服务器对 Linux 机器中的其他计算机公开? - How to make python server public to other computers in a Linux machine? 如何找到根目录以便路径可以在多台计算机上运行? - How do you find the root directory so that a path works across multiple computers? Python-为什么我的列表未在此方法范围之外进行修改? 如何修改我的代码? - Python - Why is my list not being modified outside the scope of this method? How do I modify my code so that it is? 如何修改 Python 中的代码以便仅对 Pandas 中的 NOT NaN 行进行计算? - How to modify code in Python so as to make calculations only on NOT NaN rows in Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM