Spaces:
Sleeping
Sleeping
come
commited on
Commit
•
9443b26
1
Parent(s):
ef59915
Update app.py
Browse files
app.py
CHANGED
@@ -5,31 +5,34 @@ import tempfile
|
|
5 |
import shutil
|
6 |
def generate_file(file_obj):
|
7 |
global tmpdir
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
def main():
|
34 |
global tmpdir
|
35 |
with tempfile.TemporaryDirectory(dir='.') as tmpdir:
|
|
|
5 |
import shutil
|
6 |
def generate_file(file_obj):
|
7 |
global tmpdir
|
8 |
+
try:
|
9 |
+
print('临时文件夹地址:{}'.format(tmpdir))
|
10 |
+
print('上传文件的地址:{}'.format(file_obj.name)) # 输出上传后的文件在gradio中保存的绝对地址
|
11 |
+
|
12 |
+
#获取到上传后的文件的绝对路径后,其余的操作就和平常一致了
|
13 |
+
|
14 |
+
# 将文件复制到临时目录中
|
15 |
+
shutil.copy(file_obj.name, tmpdir)
|
16 |
+
|
17 |
+
# 获取上传Gradio的文件名称
|
18 |
+
FileName=os.path.basename(file_obj.name)
|
19 |
+
|
20 |
+
# 获取拷贝在临时目录的新的文件地址
|
21 |
+
NewfilePath=os.path.join(tmpdir,FileName)
|
22 |
+
print(NewfilePath)
|
23 |
+
|
24 |
+
# 打开复制到新路径后的文件
|
25 |
+
with open(NewfilePath, 'rb') as file_obj:
|
26 |
+
|
27 |
+
#在本地电脑打开一个新的文件,并且将上传文件内容写入到新文件
|
28 |
+
outputPath=os.path.join(tmpdir,"New"+FileName)
|
29 |
+
with open(outputPath,'wb') as w:
|
30 |
+
w.write(file_obj.read())
|
31 |
+
|
32 |
+
# 返回新文件的的地址(注意这里)
|
33 |
+
return outputPath
|
34 |
+
except:
|
35 |
+
return ''
|
36 |
def main():
|
37 |
global tmpdir
|
38 |
with tempfile.TemporaryDirectory(dir='.') as tmpdir:
|