1 year ago

#73090

test-img

Spencer Cody

Python: How do I pass a file path as parameter to a function without defining it as a string before putting it into the function?

My end goal is to have a user copy and paste a file path into a PySimpleGUI without having to put quotes around the file path.

Here is the a brief summary of the function:

def fun1(filepath):
normal = os.path.normpath(filepath)
raw_data = pd.read_csv(normal)
# more code
return X_Angle, Y_Angle

Then here is the function implemented into PySimpleGui

 sg.theme('BluePurple')

layout = [[sg.Text('paste filepath'), sg.Text(size=(15,1), key='-OUTPUT-')],
          [sg.Input(key='-IN-'), sg.Text(size=(15,1), justification='center'), sg.FileBrowse()],
          [sg.Text(key='-BING-'), sg.Text(size=(2,1)), sg.Text('(X_Angle, Y_Angle)')],
          [sg.Button('Show'), sg.Button('Exit')]]

window = sg.Window('Pattern 2B', layout)

while True:  # Event Loop
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Show':
        window['-BING-'].update(fun1(values['-IN-']))
        
      
        # Update the "output" text element to be the value of "input" element
       
    

window.close

The error I get is "PermissionError: [Errno 13] Permission denied: '.'" Which I believe is just do to the backslashes in the file path. The function works fine when I copy and paste the filepath into the function with quotes around it and r in front of it so its read as a raw string. However, my goal is to make this so the user can just copy and go.

python

filepath

rawstring

0 Answers

Your Answer

Accepted video resources