有没有办法执行文件仅知道子目录?

发布于 2025-01-24 10:53:05 字数 378 浏览 2 评论 0原文

我有两个EXE文件 C:\ Users \ Bella \ Desktop \文件夹\ Fuctions
我想编写一个python脚本,可以从桌面\文件夹中打开两个脚本,问题是我想与人分享此脚本,我不知道他们的用户名称, 有没有一种方法可以执行文件,而仅知道它将在文件夹\ function中,因为我总是知道我的脚本会位于“文件夹”中,

import os
os.startfile(r"C:\Users\Bella\Desktop\Folder\Dist\fuctions\Test.exe")
os.startfile(r"C:\Users\Bella\Desktop\Folder\Dist\fuctions\Test2.exe")

显然是在与朋友共享时无法工作

I have two exe files located at
C:\Users\Bella\Desktop\Folder\fuctions
I want to write a python script that would open them both from Desktop\Folder, The issue is I want to share this script with people and I won't know the name of their User,
Is there a way to execute the files while only knowing it will be in Folder\functions since I will always know that my script will be located in "Folder"

import os
os.startfile(r"C:\Users\Bella\Desktop\Folder\Dist\fuctions\Test.exe")
os.startfile(r"C:\Users\Bella\Desktop\Folder\Dist\fuctions\Test2.exe")

obviously that wont work when shared to friends

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫卆 2025-01-31 10:53:05

您可以使用评论中提到的相对路径或使用:

import os

# get the directory of the file
dir = os.path.realpath('...')

# append the remainder of the path
path = os.path.join(dir, 'Dist\fuctions\Test.exe')

You can use relative path as mentioned in the comments or use:

import os

# get the directory of the file
dir = os.path.realpath('...')

# append the remainder of the path
path = os.path.join(dir, 'Dist\fuctions\Test.exe')
開玄 2025-01-31 10:53:05

使用os.environ.get(“用户名”)
它获得了用户的用户名。

import os
username = os.environ.get("USERNAME")
os.startfile(f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test.exe")
os.startfile(r=f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test2.exe")

或者,您可以使用os.getLogin()

import os
username = os.getlogin()
os.startfile(f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test.exe")
os.startfile(r=f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test2.exe")

upvote(如果有效):)

Use os.environ.get("USERNAME")
It gets the username of the user.

import os
username = os.environ.get("USERNAME")
os.startfile(f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test.exe")
os.startfile(r=f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test2.exe")

Or you can use os.getlogin()

import os
username = os.getlogin()
os.startfile(f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test.exe")
os.startfile(r=f"C:\\Users\\{username}\\Desktop\\Folder\\Dist\\fuctions\\Test2.exe")

Upvote if it works :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文