Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get filename from path

import os
print(os.path.basename(your_path))
Comment

getting the file path of a file object in python

>>> import os
>>> f = open('file.txt')
>>> os.path.realpath(f.name)
Comment

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 get filename from path

print (os.path.basename("/path/to/file.txt"))
# file.txt
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

Find path to the given file using Python

import os
 
print('Absolute path of file:     ',
      os.path.abspath(__file__))
print('Absolute directoryname: ',
      os.path.dirname(os.path.abspath(__file__)))
Comment

PREVIOUS NEXT
Code Example
Python :: python remove first item in tuple 
Python :: python3 change file permissions 
Python :: django models using Value 
Python :: how to do element wise multiplication in numpy 
Python :: port 5432 failed: Connection timed out (0x0000274C/10060) Is the server running on that host and accepting TCP/IP connections? 
Python :: starting vscode on colab 
Python :: wolfram alpha python module 
Python :: python check if string is int 
Python :: python not jump next line 
Python :: pandas transpose 
Python :: pytube progress bar example 
Python :: calculating mean for pandas column 
Python :: get first line of file python 
Python :: python currency 
Python :: AttributeError: __enter__ python 
Python :: add to middle of list python 
Python :: reverse geocode python 
Python :: pandas groupby aggregate multiple columns 
Python :: python dict print keys 
Python :: install virtual environment python mac 
Python :: python float to 2 decimals 
Python :: dataframe nested json 
Python :: python api define bearer token 
Python :: Python Creating string from a timestamp 
Python :: flip key and value in dictionary python 
Python :: how to tell if member is a bot discord.py 
Python :: count the number of rows in a database table in Django 
Python :: how to switch driver in python selenium 
Python :: python get cookie from browser 
Python :: or operator in django queryset 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =