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
#1
Hi,

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,

FILENAME "SOMEFILENAME",TYPE,0,165,"5"

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 : " , inputfile.name)     // trying to print file name only without extension //
        filedata = inputfile.read()
        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:
            file.write(filedata)
Reply
#2
Here are a few options:
filename = Path(...)
assume filename = /.../.../myfile.txt
to get just filename: print(filename.name) 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add product details in exe generated by pyinstaller arex786 1 173 Oct-10-2021, 11:00 AM
Last Post: Sran012
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 261 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 2,177 May-07-2021, 03:26 PM
Last Post: herwin
  How to make a test data file for the full length of definition? MDRI 6 1,080 Apr-16-2021, 01:47 AM
Last Post: MDRI
  Add file to sys.path permanently hcccs 5 1,618 Jan-31-2021, 11:26 AM
Last Post: hcccs
  find file not knowing the extension Leon79 6 1,192 Jul-07-2020, 04:44 PM
Last Post: Leon79
  Can't open/read txt file in C extension for Python Rad226 8 2,012 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,319 May-29-2020, 02:40 PM
Last Post: DarkCoder2020
  PyDrive download file path MiniMinnow 0 1,721 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  String to File Path creedX 4 1,259 Apr-06-2020, 07:29 PM
Last Post: creedX

Forum Jump:

User Panel Messages

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