Jan-29-2020, 02:28 PM
(Jan-29-2020, 02:06 PM)buran Wrote: using os module functionsBuran Thank you for the swift reply. My problem is that I cannot incorporate this to my code. If you see the code I posted the deletion of .csv extension from the initial file has to happen in the with open statement
>>> import os >>> os.path.split(r'c:\some_folder\some_file.csv') ('c:\\some_folder', 'some_file.csv') >>> root, file = os.path.split(r'c:\some_folder\some_file.csv') >>> os.path.join(root, 'new_file.csv') 'c:\\some_folder\\new_file.csv' >>> root, ext = os.path.splitext(r'c:\some_folder\some_file.csv') >>> root 'c:\\some_folder\\some_file' >>> '_'.join((root, 'clean.csv')) 'c:\\some_folder\\some_file_clean.csv'or using pathlib module
>>> import pathlib >>> p = pathlib.Path(r'c:\some_folder\some_file.csv') >>> p.with_name('new_file.csv') WindowsPath('c:/some_folder/new_file.csv') >>> p.parent.joinpath(''.join((p.stem, '_clean.csv'))) WindowsPath('c:/some_folder/some_file_clean.csv') >>>
with open(('{}_clean.csv').format(filename),'a+') as cl:also I need only the filename to change and not the path because the code will work in the same path where the 2 files are. So what I need -if it's at all possible - it's something that works with string formatting inside the .format(filename).
Again thank you for your help and your time.