简体   繁体   English

如何从python中的不同文件夹,具有不同文件扩展名或不同文件类型的文件导入并运行它们而没有错误?

[英]How to import a file from different folder and with different file extensions or different file types in python and run them without error?

I'm trying to import a file called comodoinit.sh from a folder called av-utility and the file is a linux shell file with extension (.sh) . 我正在尝试从名为av-utility的文件夹中导入名为comodoinit.sh的文件,并且该文件是扩展名为(.sh)的Linux shell文件。 I want to import the comodoinit.sh file and run them without error in python . 我想导入comodoinit.sh文件并在python中运行它们而不会出现错误。 The python file name is controlunit.py . python文件名为controlunit.py。

I tried to import it using the module sys and then import the folder and then import the module . 我尝试使用模块sys导入它,然后导入文件夹,然后导入模块。 But that didn't workout properly . 但这不能正确地锻炼。 The interpreter displays that no module named comodoinit.sh 解释器显示没有名为comodoinit.sh的模块

import sys
sys.path.insert (1, 'D:\Projects\Codes\Projects\Sleek Security\Linux Security\av-utility')
import comodoinit.sh

I need to import the comodoinit.sh file and i need to run that file with another linux shell file without no errors in just one execution . 我需要导入comodoinit.sh文件,并且需要使用另一个linux shell文件运行该文件,而在一次执行中没有任何错误。

your situation looks ambiguous 你的情况看起来模棱两可
import is for .py files, not .sh 导入适用于.py文件,而不适用于.sh
.sh files are for execution in linux .sh文件在Linux中执行
but your path tells you are coding in windows 但是您的路径告诉您在Windows中进行编码

in linux you can do os.popen('/path/to/comodoinit.sh') to execute a shell script 在Linux中,您可以执行os.popen('/ path / to / comodoinit.sh')执行Shell脚本
in windows you need to convert the .sh file to equivalent .bat file(not always possible) and do os.popen on that .bat file 在Windows中,您需要将.sh文件转换为等效的.bat文件(并非总是可能),并对该.bat文件执行os.popen

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM