1

So I know how to get a location of the needed file, I'm doing this with

file_name = os.path.splitext(os.path.basename(file_full_name))[0] 

but what I need to do is:

  1. Get the file name, which I have
  2. Get path of the file, which I also know how to get
  3. Create a file in the same directory with a modified name, for example lets say I have a file called "data.csv" on Desktop. I would need to create a file called "data - results.csv" to Desktop.

I have only tried printing the new name, but the only result I have got with this code:

 myresultcsvfile = os.path.splitext(os.path.basename(file_name))[0] + " - results.csv" 

is this:

myfile: ('Book1 - Copy', ' - results.csv') 

I'm clearly doing something wrong but I cant figure out what. And that's just the file name, I also need to add full path of the parent file to it (so that the end result would be "C:\users[username]\desktop\Book1 - copy - results.csv" in this case)

1
  • 1
    Have you tried just os.path.splitext(file_full_name)[0] + " - results.csv"? Commented Oct 5, 2017 at 15:29

1 Answer 1

1

Try this:

full = 'C:\\ .. put here your path .. \\data.csv' dir_name = os.path.dirname(full) file_name = os.path.splitext(os.path.basename(full))[0] output_file = dir_name + '\\' + file_name + ' - results.csv' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.