Search code examples
pythonpython-3.xoperating-systemrelative-pathpdfkit

Relative path not pointing to desired directory


I have the following code:

import datetime as date
import os
import pdfkit

import getpass #Gets me current username

username = getpass.getuser()

path = f"/home/{username}/Data"
relative_path = os.path.relpath(path, os.getcwd())
destination = os.path.join(relative_path, 'data.pdf')
pdfkit.from_url('www.google.com', f'{destination}/data.pdf')

I want the pdf to be saved in windows equivalent of /home/[username]/datafolder. I don't really need to use use linux or mac but for academic reasons i have decided to use the relative path method.

This code makes sense to me but for some reason it is not the directory i want it to be because when i specify the path this way the pdf generator, generates an error.

Error: Unable to write to destination                              
Exit with code 1, due to unknown error.

I know the error is in the last line of code where i have specified '/relative_path/data.pdf'. Could you please advise how i can resolve this issue?


Update 1:

As suggested by @Matthias I have updated the code but I am still getting the same error

Update 2:

I tried:

from pathlib import Path
destination = Path.home()
try:  
    os.mkdir(destination\Data)  
except OSError as error:  
    print(error)  

But it is still not pointing to the directory Data


Update 3 I know i am getting closer:

import pdfkit
import datetime as date
import calendar
import os.path
import getpass

username = getpass.getuser()

path = f"/home/{username}/Data"
os.makedirs(relative_path, exist_ok=True)
#start = os.getcwd()
relative_path = os.path.relpath(path, os.getcwd())
destination = os.path.join(relative_path, 'data.pdf')

pdfkit.from_url('www.google.com', f'{relative_path}/data.pdf')

At this point the code is executes but the folder Data was not created not am i able to locate data.pdf. I did get sucessful run though:

Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done    

Any ideas on how i can get this working correctly? The code does not produce the folder or the file?


Solution

  • Just check by putting relative_path line before os.makedirs

    As below

    import pdfkit
    import datetime as date
    import calendar
    import os.path
    import getpass
    
    username = getpass.getuser()
    
    #path = os.path.join("home","{username}","Data")
    # in case of window you will need to add drive "c:" or "d:" before os.path.sep
    path = os.path.join(,"home",username,"Data")
    relative_path = os.path.relpath(path, os.getcwd())
    os.makedirs(relative_path, exist_ok=True)
    #start = os.getcwd()
    destination = os.path.join(relative_path, 'data.pdf')
    
    pdfkit.from_url('www.google.com', f'{relative_path}/data.pdf')