保存位置 - 来自 Python 个打开和写入函数的文件

Save location - Files from Python open and write functions

我正在尝试将文件从“One Drive”同步到我自己的桌面。

我正在使用以下代码片段将文件从一个驱动器下载到桌面:


if response.status_code == 200:
    print('\n> Response Success')

    with open('Book2.xlsx', 'wb') as File:
        File.write(response.content)
        
        #print(File(type))
        #df = pd.DataFrame(File)
        #print(df)
        
        #df.to_excel(r'C:\Users\s.gaur\Desktop\Test.xlsx')
        
        print('\n> File Downloaded')
else:
    print('\n> Failed:', response.status_code)
    print(response.content)

正在下载文件,但我无法理解在本地计算机上的何处可以找到该文件。如果它来自 One Drive 的同步,请帮助我它的位置。

谢谢

通常文件会保存到脚本的父目录中,您可以 运行

import os
os.getcwd()

获取父目录。 您还可以在文件指针创建语句中使用绝对路径 即

with open('/tmp/Book2.xlsx', 'wb') as File:

那么很明显它正在将其保存到 /tmp/Book2.xlsx

在 windows 你需要使用 \ 或使用 os.path.join 功能,即

os.path.join('tmp','Book2.xlsx')

gl 高频