bdshare.py 支持文件夹,并且对文件夹内采用相对位置

This commit is contained in:
2020-10-08 11:51:04 +08:00
parent da28e20514
commit d775342166
2 changed files with 39 additions and 6 deletions

3
.gitignore vendored
View File

@@ -127,3 +127,6 @@ dmypy.json
# Pyre type checker
.pyre/
.vscode/
*.txt

View File

@@ -14,22 +14,49 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from os.path import exists, getsize, split
from os.path import exists, getsize, split, isdir, isfile, relpath
from os import listdir
from hashlib import md5
import sys
from time import time
def listdirc(path: str, path2: str = None):
if path2 is None and path[-1] != '/' and path[-1] != '\\':
path2 = path + '/'
elif path2 is None:
path2 = path
l = listdir(path)
r = []
for i in l:
fn = f'{path}/{i}'
if exists(fn):
if isfile(fn):
r.append({'fn': fn, 'path': path2})
elif isdir(fn):
r = r + listdirc(fn, path2)
return r
limit_size = 256 * 1024
if __name__ == "__main__":
if len(sys.argv) > 1:
file_list = sys.argv[1:]
filel = sys.argv[1:]
else:
print('未输入文件')
exit(-1)
filel = ['.']
file_list = []
for i in filel:
if exists(i):
if isfile(i):
file_list.append({'fn': i, 'path': ''})
elif isdir(i):
file_list = file_list + listdirc(i)
e = 1
l = len(file_list)
for i in file_list:
f2 = open('bdshare.txt', 'w', encoding='utf8')
for j in file_list:
i = j['fn']
if exists(i):
fs = getsize(i)
with open(i, 'rb', 1024) as f:
@@ -50,5 +77,8 @@ if __name__ == "__main__":
md52 = md.hexdigest()
if fs < limit_size:
md51 = md52
print(f"\r[{e}/{l}]:{md52}#{md51}#{fs}#{split(i)[1]}")
share = f'{md52}#{md51}#{fs}#{relpath(i,j["path"])}'
f2.write(share+'\n')
print(f"\r[{e}/{l}]:{share}")
e = e + 1
f2.close()