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