Skip to content

Commit

Permalink
支持相对路径,支持不在脚本路径使用脚本 (#8)
Browse files Browse the repository at this point in the history
* Update main.py

* Update main.py

* Update main.py

* Update AliyunDrive.py

* Update common.py

* Update main.py

* Update main.py

* Update common.py
  • Loading branch information
w311ang authored May 17, 2021
1 parent ee2791c commit e01a8ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions AliyunDrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def token_refresh(self):
try:
post_json = post.json()
# 刷新配置中的token
with open(os.getcwd() + '/config.json', 'rb') as f:
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json', 'rb') as f:
config = json.loads(f.read().decode('utf-8'))
config['REFRESH_TOKEN'] = post_json['refresh_token']
with open(os.getcwd() + '/config.json', 'w') as f:
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json', 'w') as f:
f.write(json.dumps(config))
f.flush()

Expand Down
6 changes: 3 additions & 3 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def date(timestamp):


def log(message):
file = os.getcwd() + '/log/' + time.strftime("%Y-%m-%d", time.localtime()) + '.log'
file = os.path.dirname(os.path.realpath(__file__)) + '/log/' + time.strftime("%Y-%m-%d", time.localtime()) + '.log'
if not os.path.exists(os.path.dirname(file)):
os.mkdir(os.path.dirname(file))
with open(file, 'a') as f:
Expand All @@ -103,7 +103,7 @@ def get_xml_tag_value(xml_string, tag_name):
def load_task():
LOCK.acquire()
try:
with open(os.getcwd() + '/tasks.json', 'rb') as f:
with open(os.path.dirname(os.path.realpath(__file__)) + '/tasks.json', 'rb') as f:
task = f.read().decode('utf-8')
return json.loads(task)
except Exception:
Expand All @@ -115,7 +115,7 @@ def load_task():
def save_task(task):
LOCK.acquire()
try:
with open(os.getcwd() + '/tasks.json', 'w') as f:
with open(os.path.dirname(os.path.realpath(__file__)) + '/tasks.json', 'w') as f:
f.write(json.dumps(task))
f.flush()
finally:
Expand Down
14 changes: 8 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# +-------------------------------------------------------------------
# | 阿里云盘上传Python3脚本
Expand Down Expand Up @@ -71,12 +72,12 @@ def upload_file(path, filepath):

# 配置信息
try:
with open(os.getcwd() + '/config.json', 'rb') as f:
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json', 'rb') as f:
config = {
"REFRESH_TOKEN": "refresh_token",
"DRIVE_ID": "drive_id",
"ROOT_PATH": "root",
"FILE_PATH": os.getcwd(),
"FILE_PATH": os.path.dirname(os.path.realpath(__file__)),
"MULTITHREADING": False,
"MAX_WORKERS": 5,
"CHUNK_SIZE": 104857600,
Expand All @@ -98,14 +99,15 @@ def upload_file(path, filepath):
raise e
# 命令行参数上传
if len(sys.argv) == 2:
if os.path.isdir(sys.argv[1]):
abspath=os.path.abspath(sys.argv[1])
if os.path.isdir(abspath):
# 目录上传
FILE_PATH = sys.argv[1]
FILE_PATH = abspath
file_list = get_all_file_relative(FILE_PATH)
else:
# 单文件上传
FILE_PATH = os.path.dirname(sys.argv[1])
file_list = [os.path.basename(sys.argv[1])]
FILE_PATH = os.path.dirname(abspath)
file_list = [os.path.basename(abspath)]
common.DATA['tasks'] = {}
else:
file_list = get_all_file_relative(FILE_PATH)
Expand Down

0 comments on commit e01a8ca

Please sign in to comment.