Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get file path

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)
Comment

python os get path

import pathlib
pathlib.Path().resolve()
Comment

get the path of a module in python

import inspect
inspect.getfile(<mdule name>)
Comment

python get file path

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-"])
Comment

PREVIOUS NEXT
Code Example
Python :: python find file name 
Python :: gspread_pandas pypi 
Python :: print in python without using print 
Python :: creating a sqlite3 table in python and inserting data in it 
Python :: group by, aggregate multiple column -pandas 
Python :: convert all colnames of dataframe to upper 
Python :: group multiple columns in pandas 
Python :: Find Files With a Certain Extension in the Directory and Its Subdirectories in Python 
Python :: python virtual enviroment 
Python :: dataframe to tf data 
Python :: python Modulo 10^9+7 (1000000007) 
Python :: 3d array into 2d array python 
Python :: if name == main 
Python :: python sockets 
Python :: pandas plot date histogram 
Python :: python import graphviz 
Python :: python loop through dictionary 
Python :: how to use input in python 
Python :: drop-trailing-zeros-from-decimal python 
Python :: negative number factor python 
Python :: how append a directory based on current directory python 
Python :: custom django user model 
Python :: new column with multiple conditions 
Python :: generate random integers 
Python :: how to create dictionary in python from csv 
Python :: Handling categorical feature 
Python :: python sum of list axes 
Python :: python run batch file 
Python :: python remove empty values from list 
Python :: check if something is nan python 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =