[Python] How can I create a full path to a file from parts (e.g. path to the folder, name and...

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    I need to pass a file path name to a module. How do I build the file path from a directory name, base filename, and a file format string?

    The directory may or may not exist at the time of call.

    For example:

    dir_name='/home/me/dev/my_reports'
    base_filename='daily_report'
    format = 'pdf'


    I need to create a string '/home/me/dev/my_reports/daily_report.pdf'

    Concatenating the pieces manually doesn't seem to be a good way. I tried os.path.join:

    join(dir_name,base_filename,format)


    but it gives

    /home/me/dev/my_reports/daily_report/pdf

    Continue reading...

Compartilhe esta Página