Python Forum
How to get file name without the full path details and without extension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get file name without the full path details and without extension

I'm new to python. Created a simple program which does search and replace (string) for a list of binary files located in given input directory and i copy the each files after replacing the string to a output directory.

I need help on two items.

item:1 (cant get only file name)
Getting each file name only for the given input directory (without the path and extension)

item2: (need some suggestion to implement this)

after i get each file name only for the given input directory, i want to update corresponding output file with file name.

for example, the output file will have a line like this,


So, i need to get the filename only and i need to update "SOMEFILENAME" in the above given line. I need to do this for each and every output files.

Below is my code i created:

tried item 1 but it prints full path followed by filename.extension but for item 2, i do not know how to implement this. suggestions please.

import os
from pathlib import Path

openfile = input('Enter the input file name: ')
outputfile = input('Enter the output file name: ')
originalName = input('Enter original  name: ')
newName = input('Enter new name to be modified: ')

# sourcepath = os.listdir('inputfiles/')
sourcepath = os.listdir(openfile)
for file in sourcepath:
    # print(openfile)
    # print(outputfile)
    # print(os.getcwd())
    inputfile = openfile + '/' + file
    # print('conversion is on-going for: ' + inputfile)
    with open(inputfile, 'rb') as inputfile:

        print("file name is : " ,     // trying to print file name only without extension //
        filedata =
        i = 0
        i = filedata.count(b'originalName')
        destinationpath = outputfile + '/' + file
        filedata = filedata.replace(bytes(originalName, 'ASCII'), bytes(newName, 'ASCII'))
        with open(destinationpath, 'wb') as file:
Here are a few options:
filename = Path(...)
assume filename = /.../.../myfile.txt
to get just filename: print( result: myfile.txt
to get full path: print(filename.resolve()) result /.../.../myfile.txt
to get just the stem: print(filename.stem result myfile
to get suffix: print(filename.suffix result: .txt
to get absolute path broken into tuples: filename.resolve().parts

Possibly Related Threads…
Thread Author Replies Views Last Post
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,082 May-07-2021, 03:26 PM
Last Post: herwin
  How to make a test data file for the full length of definition? MDRI 6 778 Apr-16-2021, 01:47 AM
Last Post: MDRI
  Add file to sys.path permanently hcccs 5 963 Jan-31-2021, 11:26 AM
Last Post: hcccs
  find file not knowing the extension Leon79 6 1,011 Jul-07-2020, 04:44 PM
Last Post: Leon79
  Can't open/read txt file in C extension for Python Rad226 8 1,651 Jun-26-2020, 04:08 PM
Last Post: Rad226
  How to append a tuple full of records to a dbf file in Python? DarkCoder2020 4 1,130 May-29-2020, 02:40 PM
Last Post: DarkCoder2020
  PyDrive download file path MiniMinnow 0 1,449 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  String to File Path creedX 4 1,031 Apr-06-2020, 07:29 PM
Last Post: creedX
  Add path to a local file in pop-up field pythonscripting 1 690 Feb-08-2020, 10:57 PM
Last Post: Larz60+
  How to get full path of specified hidden files matching pattern recursively SriRajesh 4 1,127 Jan-18-2020, 07:12 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020