吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1449|回复: 30
收起左侧

[求助] 类unbuntu下sh文件调用.py文件始终不执行

[复制链接]
1e3e 发表于 2022-2-22 11:33
本帖最后由 1e3e 于 2022-2-22 12:22 编辑

写了一个py脚本如下:
#!/usr/bin/python
import time
import os
t=time.gmtime()
shijian=time.strftime("%Y%m%d",t)

def ListFilesToTxt(dir, file, wildcard, recursion):
    exts = wildcard.split(" ")
    files = os.listdir(dir)
    for name in files:
        fullname = os.path.join(dir, name)
        if (os.path.isdir(fullname) & recursion):
            ListFilesToTxt(fullname, file, wildcard, recursion)
        else:
            for ext in exts:
                if (name.endswith(ext)):
                    file.write(name + "-" + shijian + "\n")
                    break


def Test():
    dir = "/home/asd/Downloads/qq-files/隐私隐去/file_recv"  # 文件路径
    outfile = "/home/asd/Downloads/qq-files/隐私隐去/file_recv/目录/用来查找文件.txt"  # 写入的txt文件名
    wildcard = ".jpg .pdf"  # 要读取的文件类型;

    file = open(outfile, "a")
    if not file:
        print("cannot open the file %s for writing" % outfile)

    ListFilesToTxt(dir, file, wildcard, 1)

    file.close()


Test()

另外写了一个sh脚本:
#!/bin/bash
cd  /home/asd/Downloads/qq-files/隐私隐去/file_recv/
dirdate=`date +%Y%m%d`_`date +%H%M%S`
mkdir $dirdate
cd $dirdate
zip  pdf.zip  /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.pdf -P 123
zip  jpg.zip  /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.jpg  -P 123
python /home/asd/test/bak.py
rm -f /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.jpg  /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.pdf
rmdir -p --ignore-fail-on-non-empty /home/asd/Downloads/qq-files/隐私隐去/file_recv/*
rsync -r --include=*.xlsx --include=*.xls --include=*.zip --include=*.ppt --include=*用来查找文件* --include=*.doc --include=*.docx  --include=*.wps --include=*.et  --filter="-! */"  /home/asd/Downloads/qq-files/隐私隐去/file_recv /data/testest

我在终端中执行“python /home/asd/test/bak.py”成功执行,成功生成/home/asd/Downloads/qq-files/隐私隐去/file_recv/目录/用来查找文件.txt,但在sh脚本中加入“python /home/asd/test/bak.py”语句始终不能执行,
求大佬解答或者大佬们不用python语言,直接sh脚本解决也可

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 1e3e 发表于 2022-2-22 11:42
没人吗?大佬呢?自己顶一下
s1986q 发表于 2022-2-22 12:33
你把sh执行的输出贴出来,我好分析。
>>> str
<class 'str'>
>>> dir
<built-in function dir>
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>>
dir是个函数名。是冲突了。
 楼主| 1e3e 发表于 2022-2-22 12:45
s1986q 发表于 2022-2-22 12:33
你把sh执行的输出贴出来,我好分析。
>>> str

已经说的很清楚了,sh内容如下:
#!/bin/bash
cd  /home/asd/Downloads/qq-files/隐私隐去/file_recv/
dirdate=`date +%Y%m%d`_`date +%H%M%S`
mkdir $dirdate
cd $dirdate
zip  pdf.zip  /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.pdf -P 123
zip  jpg.zip  /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.jpg  -P 123
rm -f /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.jpg  /home/asd/Downloads/qq-files/隐私隐去/file_recv/*.pdf
rmdir -p --ignore-fail-on-non-empty /home/asd/Downloads/qq-files/隐私隐去/file_recv/*
rsync -r --include=*.xlsx --include=*.xls --include=*.zip --include=*.ppt --include=*用来查找文件* --include=*.doc --include=*.docx  --include=*.wps --include=*.et  --filter="-! */"  /home/asd/Downloads/qq-files/隐私隐去/file_recv /data/testest
以上sh脚本内容全部正常执行!!!!!!!

加上python /home/asd/test/bak.py这句后,除了python /home/asd/test/bak.py没起作用,其它语句执行正常!!!!!
 楼主| 1e3e 发表于 2022-2-22 12:56
s1986q 发表于 2022-2-22 12:33
你把sh执行的输出贴出来,我好分析。
>>> str

实在 不行,你帮我远程一下电脑吧
qqLinux 发表于 2022-2-22 13:32
一般这种情况用 pyinstaller 打包成二进制运行。或者使用shell的多进程机制调用 python 的命令行
jixuewen 发表于 2022-2-22 13:54
1、sh中引入path,把当前用户echo $PATH看到的结果都引入
2、bash -x  你的sh脚本
这样执行看看。
应当是环境变量没有带过来。
s1986q 发表于 2022-2-22 14:20
本帖最后由 s1986q 于 2022-2-22 14:27 编辑

你的变量值中文很多,我试过,就没输出。
最前面的没删,你把所有dir变量换了,就好了。
zheng10072 发表于 2022-2-22 15:02
大概率是环境变量问题
python /home/asd/test/bak.py  这个 python 用绝对路径试试,类似于:/usr/bin/python /home/asd/test/bak.py
 楼主| 1e3e 发表于 2022-2-22 15:10
zheng10072 发表于 2022-2-22 15:02
大概率是环境变量问题
python /home/asd/test/bak.py  这个 python 用绝对路径试试,类似于:/usr/bin/pyt ...

右键终端 输入python /home/asd/test/bak.py ,执行结果是正常的,有点不解
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 22:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表