对于想了解Python获取可执行文件的路径?的读者,本文将是一篇不可错过的文章,我们将详细介绍python获取可执行文件的路径信息,并且为您提供关于.net–如何获取可执行文件的版本?、C#如何获取可
对于想了解Python获取可执行文件的路径?的读者,本文将是一篇不可错过的文章,我们将详细介绍python获取可执行文件的路径信息,并且为您提供关于.net – 如何获取可执行文件的版本?、C# 如何获取可执行文件路径的上上级目录、C# 获取可执行文件路径的上上级目录的方法、c++可执行文件 管道 python 管道 c++可执行文件的有价值信息。
本文目录一览:- Python获取可执行文件的路径?(python获取可执行文件的路径信息)
- .net – 如何获取可执行文件的版本?
- C# 如何获取可执行文件路径的上上级目录
- C# 获取可执行文件路径的上上级目录的方法
- c++可执行文件 管道 python 管道 c++可执行文件
Python获取可执行文件的路径?(python获取可执行文件的路径信息)
如何解决Python获取可执行文件的路径??
我对 Python 编程非常陌生,我已经拼凑了一些代码,希望可以自动重命名特定文件夹中的一堆文件。我需要在没有安装 Python 的计算机上运行这个程序(它没有连接到互联网,我无法在上面安装 Python)所以我使用 auto-py-to- 将 .py 文件转换为 .exe 文件 -可执行程序。 .py 文件在我的笔记本电脑上工作,所以只有当我尝试运行 .exe 文件时才会出现问题。
我在整个代码中使用脚本所在文件夹的路径,因此在脚本的开头,我将文件路径保存为“路径”:
path = os.path.dirname(os.path.realpath(__file__))
但是,当我将 .py 文件转换为 .exe 文件并将该 .exe 文件移动到另一个文件夹时,保存在“路径”变量中的文件路径位于临时文件夹中(可能是文件转换器)。我通过隔离获取文件路径的脚本部分并制作一个 .exe 文件来仅打印该文件路径来测试它。
有没有其他方法可以获取可执行文件的文件路径?我不想将文件路径硬编码到程序中,因为人们经常重新组织该计算机上的文件夹,而我想要这个程序要灵活。
如果上下文需要,这里是其余的代码。谢谢,如果这是一个非常基本的问题,我很抱歉:)
# This section of code imports the modules used below
import os
import tkinter as tk
from tkinter import simpledialog
from tkinter import messageBox
# Setting global variables
path = os.path.dirname(os.path.realpath(__file__)) + "/" # This sets the filepath to the current folder
# Choosing the dough
dough_input = tk.Tk(className=''Select dough'')
dough_input.geometry(''400x200'')
# This is the code that creates the dough selection Box. Any new types can be added here using the template below:
dough = tk.StringVar()
radiobutton_1 = tk.Radiobutton(dough_input,text=''a'',variable=dough,value="a",tristatevalue=0)
radiobutton_1.pack()
radiobutton_2 = tk.Radiobutton(dough_input,text=''b'',value="b",tristatevalue=0)
radiobutton_2.pack()
radiobutton_3 = tk.Radiobutton(dough_input,text=''c'',value="c",tristatevalue=0)
radiobutton_3.pack()
radiobutton_4 = tk.Radiobutton(dough_input,text=''d'',value="d",tristatevalue=0)
radiobutton_4.pack()
# This section of code saves the dough chosen and closes the pop-up window.
def submitfunction():
global bread
bread = dough.get()
dough_input.destroy()
submit = tk.Button(dough_input,text=''Submit'',command=submitfunction)
submit.pack()
# This section of code triggers the pop-up window for the dough selection.
dough_input.mainloop()
# Inputting the first number
root = tk.Tk()
root.withdraw()
user_inp = simpledialog.askinteger("Number Input","Input first number:")
i = int(user_inp)
# Defining the file rename function
def rename():
global i # use the variable "i" defined above (The number that the user inputs)
for filename in os.listdir(path): # for each file in the folder specified above do the following
if ".sur" in filename:
my_dest = str(bread) + " " + str(i).zfill(4) + ".txt" # sets the new filename
my_source = path + filename # Defines the old filepath to the file
my_dest = path + my_dest # Defines the new filepath to the file
os.rename(my_source,my_dest) # rename function
i = i + 1 # advances the program down the list
messageBox.showinfo("Success","All files have been renamed successfully!") # confirmation message!
# This function asks the user if the information they''ve input is correct. Once the user clicks "ok" the program runs
# the rename function.
def sanity_check():
file_list = os.listdir(path)
j = 0
for g in file_list:
if ".txt" in g:
j += 1
# This section of code does some calculations and creates a "sanity check" for the user. It takes the number that
# the user inputted above and sets that as the first number "i". Then it counts the number of files that
# meets the above criteria and sets that to the number of files. It uses that number of files to calculate the
# last number in the list. It then concatenates all this information and asks the user to
# confirm. If the user presses "ok" then the program
# proceeds with the rename. If the user presses cancel,the program aborts.
first_num = i
num_files = j
length_files = int(num_files) - 1
last_file = i + length_files
last_num = last_file
check = str(
"There are " + str(num_files) + " files in this folder. The dough analyzed is " + str(
dough) + ". The first "
"number is " + str(first_num).zfill(
4) + " and the last number is " + str(
last_num).zfill(4) + ". Is this correct?")
# This is the section of code that asks the user to verify the numbers.
confirmation = messageBox.askokcancel("Sanity Check!",check)
if confirmation == False:
messageBox.showerror("","Program Cancelled")
else:
rename()
# This is the command that runs the program.
sanity_check()
解决方法
我认为,os.getcwd()
和 sys.executable
正是您要找的。
使用这 2 个函数,您将知道脚本运行所在的文件夹(从您启动它的位置),以及当前 python 可执行文件的路径(可能包含在 .exe 发行版中,并解压到 tmp 目录)
➜ ~ cd /tmp/
➜ /tmp mkdir x
➜ /tmp cd x
➜ x python
Python 3.9.5 (default,May 24 2021,12:50:35)
[GCC 11.1.0] on linux
Type "help","copyright","credits" or "license" for more information.
>>> import os
>>> os.getcwd()
''/tmp/x''
>>> import sys
>>> sys.executable
''/usr/bin/python''
>>>
.net – 如何获取可执行文件的版本?
如何在不使用像reshacker这样的外部工具的情况下在vb.net中执行此操作?
(我找到了this link,但它是另一种语言.)
解决方法
Private Function GetFiLeversionInfo(ByVal filename As String) As Version Return Version.Parse(FiLeversionInfo.GetVersionInfo(filename).FiLeversion) End Function
用法:
Debug.WriteLine(GetFiLeversionInfo("C:\foo\bar\myapp.exe").ToString)
输出:
4.2.9.281
C# 如何获取可执行文件路径的上上级目录
1、
DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", Application.StartupPath));
di.FullName
就是你想要的
..\有几个就是往回退几层
2、
DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
String path = info.Parent.Parent.FullName;
3、
string WantedPath = Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf(@"\"));
C# 获取可执行文件路径的上上级目录的方法
1、
DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", Application.StartupPath));
di.FullName
就是你想要的
..\有几个就是往回退几层
2、
DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
String path = info.Parent.Parent.FullName;
3、
string WantedPath = Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf(@"\"));
4.
string filePath = AppDomain.CurrentDomain.BaseDirectory + @"\..\测试\1.xml";
实际路径是(AppDomain.CurrentDomain.BaseDirectory的上一层)\测试\1.xml
退几层就是几个..\
c++可执行文件 管道 python 管道 c++可执行文件
在centos中使用管道进行参数的传输
运行格式 ./c++A | python3 p.py | ./c++B
python脚本中使用标准输入和输出 stdin 和 stdout,python脚本参数是传输不到./c++B可执行文件的。
python 脚本中要使用
while True:
line=input()
............
print(line)
不要写
for line in sys.stdin:
print(line)
本人没有学过python,具体什么原因也不清楚,有请python大佬们给个解释吧
今天的关于Python获取可执行文件的路径?和python获取可执行文件的路径信息的分享已经结束,谢谢您的关注,如果想了解更多关于.net – 如何获取可执行文件的版本?、C# 如何获取可执行文件路径的上上级目录、C# 获取可执行文件路径的上上级目录的方法、c++可执行文件 管道 python 管道 c++可执行文件的相关知识,请在本站进行查询。
本文标签: