set path to curren folder and text folder in it in python

To set the path to the current folder and a text folder in it, we can use the os module in Python for file handling.

First, we need to import the os module:

main.py
import os
10 chars
2 lines

Then, we can use the os.getcwd() function to get the current working directory:

main.py
current_directory = os.getcwd()
32 chars
2 lines

Next, we can use os.path.join() function to join the current directory with the text folder name to get the path of the text folder:

main.py
text_folder = os.path.join(current_directory, 'text')
54 chars
2 lines

Now we can use text_folder variable to access the files inside the text folder.

Here's the final code block:

main.py
import os

# get the current working directory
current_directory = os.getcwd()

# join the current directory with the text folder name to get the path
text_folder = os.path.join(current_directory, 'text') 

# use text_folder variable to access the files inside the text folder
277 chars
10 lines

gistlibby LogSnag