-2

I wrote a single python file that should create a csv file in the local directory when executed. The python file does not create a csv file in the local directory, even though execution completes successfully. My code and output screenshot are included below:

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import os
def web_scraper(self):
    os.chdir(r"/Users/bsulin/PycharmProjects/StockNewsWebScraper/")

    print("Hi print")

    return("Hi return")

Pycharm Project Structure as Source Folder

Code Output

5
  • It seems that your question is just "[how to] create a csv file in the local directory". There is no need for web scraping, nor most of the other malformed code, to this question. Please reduce your code to a minimal reproducible example, both to help us understand and answer your question, and to help you realise potential errors yourself. See the How to Ask for details. Commented Mar 19, 2020 at 7:52
  • Ok thanks, I've updated my question with your feedback. Commented Mar 19, 2020 at 16:16
  • That's not what I requested. You have also removed the csv writing code. Do you actually call web_scraper, though? Commented Mar 19, 2020 at 16:50
  • It works now! I think I needed to remove the function from the class. So doesn't look like web_scraper function was being called. Commented Mar 19, 2020 at 17:00
  • Does this answer your question? How can I change directory with Python pathlib Commented May 15, 2021 at 2:36

1 Answer 1

0

It's most likely saving it somewhere you're not expecting due to a current working directory (cwd) issue.

Either change the current working directory in PyCharm as per PyCharm current working directory, or explicitly change it with code:

import os # put these lines at the top of your script
os.chdir(r"/Users/bsulin/PycharmProjects/StockNewsWebScraper/")
Sign up to request clarification or add additional context in comments.

4 Comments

I tried both of those methods (updated question with details), and it's still not working. Won't even print or return my "Hi" strings.
That's very strange. Try creating a new project in PyCharm and just put print("test"), see if that works.
It works now! I think I needed to remove the function from the class.
Oh I think what happened was that you created the function web_scraper, but you never called it, so it was never executed.

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.