0

My intention for the code bellow is to save a file inside a folder. But the file is beeing saved on the folder before the correct one.

How can i modify it so it saves the file to correct folder?

    #Make Selenium check the chatroom name
    chatroom = driver.find_element_by_class_name('_19vo_').text

    #Create a variable of a folder path with chatroom name
    chat_path = 'D:\Drive\Outros\Python\data_save\chats\ ' + chatroom

    #Verify if folder already exists, if not, create it
    Path(chat_path).mkdir(parents=True, exist_ok=True)

    #Save a df.csv to the chatroom folder
    df.to_csv(chat_path +chatroom+' in'+ timestr +' .csv', index=None, header=True, encoding='utf-8-sig')

5
  • Why chat_path +chatroom in the to_csv call ? chat_path already ends with chatroom. Commented Feb 17, 2020 at 15:07
  • I want the CSV to have the chatroom name too Commented Feb 17, 2020 at 15:11
  • 1
    Oh, so are you missing a directory separator ? df.to_csv(chat_path + '/' +chatroom+' in'+ timestr +' .csv', index=None, header=True, encoding='utf-8-sig') Commented Feb 17, 2020 at 15:16
  • It worked. Thanks! If you want to send an formal answer i will be happy to accept it! Commented Feb 17, 2020 at 15:20
  • use .joinpath Path(chat_path).joinpath(f'chatroom in {timestr}.csv') Commented Feb 17, 2020 at 15:29

2 Answers 2

1

You are missing a directory separator:

df.to_csv(chat_path + '/' +chatroom+' in'+ timestr +' .csv', index=None, header=True, encoding='utf-8-sig')

The best practice is to use os.path.join:

import os
chat_filename = '%s in%s.csv' % (chatroom, timestr)
df.to_csv(os.path.join(chat_path, chat_filename), index=None, header=True, encoding='utf-8-sig')
Sign up to request clarification or add additional context in comments.

Comments

0

Can you try using double \ in your path.

 chat_path = 'D:\\Drive\\Outros\\Python\\data_save\\chats\\ ' + chatroom

1 Comment

Just tried, it did not save the file in to the folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.