I need to make a program and require a upload file function in it, how do i do it?

42 views Asked by At

I am using customtkinter and shutil, I have made the basic functionality and the files are uploaded but only when I use Definite path, it does not work when I use a relative path I want my program to work on any device, so obviously I can't use definite path. Is there any way I can bypass this? Like any module which can give me definite path in different computers so that I can use that or credentials which i can add in a f-string or another module which functions which relative path like shutil.

The code is something like this:

status=customtkinter.CTkLabel(master=Add_student,width=200,height=250,text="",image=uploadpending,font=("Poppins Regular", 24))
  
def upload_file():
    file_path = filedialog.askopenfilename()
    if file_path:
        try:
            shutil.copy2(file_path, uplpath)
            status.configure(image=Assetsuccess)
        except Exception as e:
            status.configure(text="Error uploading file: " + str(e),image=uploadfailed)

here if I put-

uplpath="Uploads"

then it shows that upload is successful but there isn't any file uploaded in the folder specified.

the file is only uploaded when I add a definite path like-

uplpath="C:\\Users\\user\\Documents\\Coding files\\project\\Admin Window\\Uploads"

1

There are 1 answers

1
im_out_of_fruit_256 On

If you want to get the directory that the other file is in in which case

os.path.dirname(file_path)

would give you the absolute path with respect to the other file.

then you could join it with uplpath

os.path.join(os.path.dirname(file_path), uplpath)