0

When run the code bellow the line os.chdir(folder_path) return a error. What's wrong?

That's my folder hierarchy:

-data
  -NotaCorretagem_60076_20181009.pdf
-output
-report
-script
   -data_extraction.py

My data_extraction.py file code:

# import modules
from tika import parser
import pathlib as pl
import os


# list of functions
def main():
    """
    Special function to invoke the functions automatically when the program is
    executed.
    """
    extract_pdf()


def extract_pdf():
    """
    A function to extract data from stock market brokerage pdf file
    """
    # ask user input file name
    file_name = input('\n Please, enter file name: ')

    # change to data folder
    folder_path = pl.Path().cwd() / 'data'
    os.chdir(folder_path)

    # read pdf file inside data folder
    pdf = parser.from_file(file_name)
    text = pdf['content']
    print(text)


if __name__ == '__main__':
    main()

Actual Result Error:

 Please, enter file name: NotaCorretagem_60076_20181009.pdf
Traceback (most recent call last):
  File "C:/dev/project/Python/broker/script/data_extraction.py", line 49, in <module>
    main()
  File "C:/dev/project/Python/broker/script/data_extraction.py", line 28, in main
    extract_pdf()
  File "C:/dev/project/Python/broker/script/data_extraction.py", line 40, in extract_pdf
    os.chdir(folder_path)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\dev\\project\\Python\\broker\\script\\data'

Process finished with exit code 1

Any help, please?

  • you are trying to get to the data folder from within the script folder (where you run your script). So you need to go to the parent folder before and then to data. So the folder path should be: `folder_path = pl.Path().cwd().parent / 'data'` Change that line and try again – freerafiki Oct 29 '19 at 10:47
  • Thanks! That's it. I will study a llittle more about `parent` folder. – Elsior Moreira Alves Junior Oct 29 '19 at 11:42

0 Answers0