Linux从python运行shell cmd,无法加载config fi

2024-04-28 13:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我在运行Debian的raspberry pi上安装了一个名为rclone的备份程序,我在shell中成功运行了cmd,将文件夹备份到google驱动器上,但每次使用python脚本拍照时,我都需要这样做,与其他人相比,我在Linux方面没有什么经验,我想如果我用

 #!/bin/sh 

或者

^{pr2}$

然后是下面的命令

rclone copy /var/www/html/camera_images pictures::folder1

然后,我将.sh文件设为可执行文件,如果我只是在文件夹中单击它并执行,但如果我尝试用

os.system('sh /home/pi/py/upload.sh')

或者

os.system(' rclone copy /var/www/html/camera_images pictures::folder1 ')

我得到了一个错误的外壳说

Failed to load config file "/root/.rclone.conf" using default - no such directory.

但是.conf应该位于/home/pi中。如果我尝试

os.system(' sh rclone copy /var/www/html/camera_images pictures::folder1 ')

我明白了

sh: 0: Cant open rclone.

如何从python运行copy cmd或脚本来执行此操作?

我就是这样安装rclone的

  1. cd

  2. wgethttp://downloads.rclone.org/rclone-v1.34-linux-arm.zip

  3. 解压缩rclone-v1.34-linux-手臂.拉链

  4. cd rclone-v1.34-linux-arm

  5. sudo cp rclone/usr/sbin/

  6. 周素多根目录:root/usr/sbin/rclone

  7. sudo chmod 755/usr/sbin/rclone

  8. sudo mkdir-p/usr/local/share/man/man1

  9. sudo cp rclone.1/usr/local/share/man/man1/

  10. sudo mandb

  11. rclone配置


Tags: osvarusrhtmlwwwshpisudo
3条回答

谢谢各位:)

我现在用它

os.system(' rclone copy  config /home/pi/.rclone.conf /var/www/html/camera_images pictures::folder1 ')

注意,如果我把sh放在开头,我会得到一个错误sh:0:cannotopenrclone,尽管我昨天读到关于在结尾放一个,:0作为返回值?不管怎样,它都可以在没有sh的情况下工作

子流程也能工作,我将使用它。在

^{pr2}$

您应该使用subprocess模块而不是os.system。在

您可以使用^{}来创建一个进程并给它一个工作目录。在

subprocess.Popen(your_command, cwd=path_to_your_executable_dir, shell=True)

(使用shell=True来传递一个简单的字符串命令。在

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of: ....

在rclone命令中使用config

来自文档: 配置字符串配置文件。(默认/home/ncw/。rclone.conf公司“)

您的命令应该如下所示: 操作系统('sh rclone copy config/home/pi/。rclone.conf公司/var/www/html/camera_images pictures::folder1')

相关问题 更多 >