import os
print(os.path.basename(your_path))
>>> import os
>>> f = open('file.txt')
>>> os.path.realpath(f.name)
import os
def read_file(file_name):
file_handle = open(file_name)
print file_handle.read()
file_handle.close()
file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir
#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)
#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)
#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)
#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
print (os.path.basename("/path/to/file.txt"))
# file.txt
import PySimpleGUI as sg
sg.theme("DarkTeal2")
layout = [[sg.T("")], [sg.Text("Choose a file: "), sg.Input(), sg.FileBrowse(key="-IN-")],[sg.Button("Submit")]]
###Building Window
window = sg.Window('My File Browser', layout, size=(600,150))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event=="Exit":
break
elif event == "Submit":
print(values["-IN-"])
import os
print('Absolute path of file: ',
os.path.abspath(__file__))
print('Absolute directoryname: ',
os.path.dirname(os.path.abspath(__file__)))