Search code examples
pythonpython-3.xonedrive

Save location - Files from Python open and write functions


I am trying to sync files from "One Drive" to my own desktop.

I am using the following code piece to download files from One drive to desktop:


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)

The file is getting downloaded, but I am not able to understand, where to find the file on my local computer. Please help me where it would be located if it's coming from sync from One Drive.

Thanks


Solution

  • usually files get saved to the parent directory of the script you can run

    import os
    os.getcwd()
    

    to get the parent dir. you can also use absolute paths in your file pointer creation statement i.e

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

    then it's obvious it's saving it to /tmp/Book2.xlsx

    in windows you need to use \ or use os.path.join functionality i.e

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

    gl hf