SCRIPT_PATH="`dirname "$SCRIPT_PATH"`/$REL_PATH"
时间: 2023-11-12 10:06:11 浏览: 41
这是一个 Bash 脚本中的变量赋值语句,用于将一个变量 `$SCRIPT_PATH` 的值修改为新的路径。
具体来说,该语句使用了 Bash 中的 `dirname` 命令来获取 `$SCRIPT_PATH` 中所包含的路径的目录部分,然后将其和 `$REL_PATH` 变量的值拼接在一起,形成一个新的路径。最后,将新路径赋值给 `$SCRIPT_PATH` 变量。
例如,如果 `$SCRIPT_PATH` 的值为 `/home/user/scripts/myscript.sh`,`$REL_PATH` 的值为 `../lib/mylib.sh`,则该语句将会将 `$SCRIPT_PATH` 的值修改为 `/home/user/scripts/../lib/mylib.sh`,即 `/home/user/lib/mylib.sh`。
需要注意的是,该语句中使用了反引号 ` 和 `"`,表示将 `dirname "$SCRIPT_PATH"` 命令的输出作为字符串插入到整个路径中。这种写法叫做命令替换,可以将一个命令的输出作为字符串赋值给一个变量。
相关问题
改进以下代码 currentpath = os.path.dirname(os.path.realpath(__file__)) time_date = '{}{}'.format(self.time_date,self.random_char(5)) contents = os.path.join(currentpath, time_date, self.ref.split('/')[-1]) ref = self.ref.split('/')[-1] private_token = self.gl.private_token path = "lib" if ref == "master": if os.path.exists(os.path.join(contents, self.name)): subprocess.call("rm -rf {} ".format(os.path.join(contents, self.name)), shell=True, cwd=contents) time.sleep(3) retcode = start.clone(int(self.project_id), ref, contents, private_token) if retcode == 0: start.clone_frontend(self.get_frontend()[0],self.get_frontend()[1], contents, private_token,self.get_frontend()[2] ) start.clone_abc(self.get_abc()[0], self.get_abc()[1], contents, private_token,"mc_abc") start.clone_model(start.get_clkrst()[0], start.get_clkrst()[1], contents, private_token,"clkrst") start.clone_model(start.get_ara()[0], start.get_ara()[1], contents, private_token,"ara") start.clone_model(start.get_wfl()[0], start.get_wfl()[1], contents, private_token,"wfl") subprocess.call("echo '*.t' >> {}".format(os.path.join(contents, self.name, ".gitignore")),shell=True) code = start.make_lib(os.path.join(contents, self.name)) rel, err = code.communicate() if "make: *** [main] Error 2" in err.decode('utf-8'): print("loading push error log") filename = os.path.join(contents, self.name, "error_make_log") subprocess.call("echo '' > {}".format(filename), shell=True, cwd=contents) start.error_make(filename, rel.decode('utf-8') ) start.error_make(filename, err.decode('utf-8') ) else: print("loading push libs") # start.push_lib(os.path.join(contents, self.name), path, ref)
以下是改进后的代码:
```python
import os
import time
import subprocess
class MyClass:
def __init__(self, time_date, ref, project_id, private_token, name, frontend, abc, clkrst, ara, wfl):
self.time_date = time_date
self.ref = ref
self.project_id = project_id
self.private_token = private_token
self.name = name
self.frontend = frontend
self.abc = abc
self.clkrst = clkrst
self.ara = ara
self.wfl = wfl
def random_char(self, y):
# 生成随机字符串
pass
def get_frontend(self):
# 获取前端代码信息
pass
def get_abc(self):
# 获取abc代码信息
pass
def get_clkrst(self):
# 获取clkrst代码信息
pass
def get_ara(self):
# 获取ara代码信息
pass
def get_wfl(self):
# 获取wfl代码信息
pass
def clone_repo(self):
current_path = os.path.dirname(os.path.realpath(__file__))
time_date = '{}{}'.format(self.time_date, self.random_char(5))
contents = os.path.join(current_path, time_date, self.ref.split('/')[-1])
ref = self.ref.split('/')[-1]
private_token = self.private_token
path = "lib"
if ref == "master":
if os.path.exists(os.path.join(contents, self.name)):
subprocess.call("rm -rf {} ".format(os.path.join(contents, self.name)), shell=True, cwd=contents)
time.sleep(3)
start = Start() # 实例化Start类
ret_code = start.clone(int(self.project_id), ref, contents, private_token)
if ret_code == 0:
start.clone_frontend(self.get_frontend()[0], self.get_frontend()[1], contents, private_token, self.get_frontend()[2])
start.clone_abc(self.get_abc()[0], self.get_abc()[1], contents, private_token, "mc_abc")
start.clone_model(start.get_clkrst()[0], start.get_clkrst()[1], contents, private_token, "clkrst")
start.clone_model(start.get_ara()[0], start.get_ara()[1], contents, private_token, "ara")
start.clone_model(start.get_wfl()[0], start.get_wfl()[1], contents, private_token, "wfl")
subprocess.call("echo '*.t' >> {}".format(os.path.join(contents, self.name, ".gitignore")), shell=True)
code = start.make_lib(os.path.join(contents, self.name))
rel, err = code.communicate()
if "make: *** [main] Error 2" in err.decode('utf-8'):
print("loading push error log")
filename = os.path.join(contents, self.name, "error_make_log")
subprocess.call("echo '' > {}".format(filename), shell=True, cwd=contents)
start.error_make(filename, rel.decode('utf-8'))
start.error_make(filename, err.decode('utf-8'))
else:
print("loading push libs")
# start.push_lib(os.path.join(contents, self.name), path, ref)
```
这里对代码进行了一些改进:
- 将获取当前路径和时间的代码合并到一起,使代码更加简洁。
- 将前端、abc、clkrst、ara和wfl的克隆操作集成到一个实例化Start类的方法中,使代码更加模块化。
- 将错误日志的处理和推送操作分别封装到Start类的方法中,使代码更加易于维护。
pyinstaller相对路径
当使用PyInstaller将Python脚本打包成可执行文件后,由于可执行文件在运行时的工作目录并不一定是脚本所在目录,因此需要使用相对路径来访问程序所需的文件。在PyInstaller中使用相对路径时,可以使用以下方法:
1. 使用os模块的相对路径函数:可以使用os模块的path.join()函数来构建相对路径。例如,如果要访问脚本所在目录下的data文件夹中的file.txt文件,可以使用以下代码:
```
import os
# 获取脚本所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))
# 构建相对路径
rel_path = "data/file.txt"
abs_file_path = os.path.join(script_dir, rel_path)
# 读取文件内容
with open(abs_file_path, "r") as f:
content = f.read()
```
2. 使用sys._MEIPASS变量:在使用PyInstaller打包时,可以使用sys._MEIPASS变量获取打包后的文件所在目录。例如,如果要访问打包后程序所在目录下的data文件夹中的file.txt文件,可以使用以下代码:
```
import sys
import os
# 获取打包后程序所在目录
bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
# 构建相对路径
rel_path = "data/file.txt"
abs_file_path = os.path.join(bundle_dir, rel_path)
# 读取文件内容
with open(abs_file_path, "r") as f:
content = f.read()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)