-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

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Bryce S
  • 59
  • 1
  • 1
  • 9
  • 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](https://stackoverflow.com/help/how-to-ask) for details. – MisterMiyagi Mar 19 '20 at 07:52
  • Ok thanks, I've updated my question with your feedback. – Bryce S Mar 19 '20 at 16:16
  • That's not what I requested. You have also removed the ``csv`` writing code. Do you actually *call* ``web_scraper``, though? – MisterMiyagi Mar 19 '20 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. – Bryce S Mar 19 '20 at 17:00
  • Does this answer your question? [How can I change directory with Python pathlib](https://stackoverflow.com/questions/41742317/how-can-i-change-directory-with-python-pathlib) – bad_coder May 15 '21 at 02:36

1 Answers1

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/")
GordonAitchJay
  • 4,640
  • 1
  • 14
  • 16