好友
阅读权限25
听众
最后登录1970-1-1
|
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脚本解决也可 |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|