#๐Ÿ”’ Working with Pathlib to set path directory to print new .txt files

19 messages ยท Page 1 of 1 (latest)

robust rune
#

I'm currently working on the project in chapter 11 of automate the boring stuff with python and I'm noticing that when I run the first version of the code it ends up creating the files on the main directory instead of in the folder that I would like it to. I am so confused I've read the documentation and watched 3 videos and can't understand why it's not doing what I want it to do.

grand ivyBOT
#

@robust rune

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lapis mango
robust rune
#

So I have my realitive directory set up as /automatetheboringstuff/randomQuizGenerator.py and I want to create the folder /automatetheboringstuff/quizes and have the script write the files to the quizes folder.

#

I've tried .joinpath() and it doesn't seem to work

lapis mango
# robust rune So I have my realitive directory set up as /automatetheboringstuff/randomQuizGen...

You want to:

  • get the full path to your code (that's called __file__)
  • get the directory part of that
  • append quizes to that
    Something like:
codepath = Path(__file__)
print(f'{codepath=}')
dirpath = codepath.parent
print(f'{dirpath=}')
quizpath = dirpath / 'quizes'
print(f'{quizpath=}')
quizfile = quizpath / f'capitalquiz(quiz_num + 1).txt'
print(f'{quizfile=}')

Run that, see how the paths are constructed.

robust rune
#

Okay that makes sense now. I didn't know about file. But the for loop that I have constructed in the original photo is still producing the files in the automatetheboringstuff folder not in the new quizes folder

lapis mango
robust rune
#

Yes i modified it a little bit:

quizFolder = Path(file).parent / 'quizes'
quizFolder.mkdir(exist_ok=True)
print(f'{quizFolder=}')
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona':
'Phoenix', 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado':
'Denver', 'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida':
'Tallahassee', 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise',
'Illinois': 'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines',
'Kansas': 'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge',
'Maine': 'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston',
'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson',
'Missouri': 'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln',
'Nevada': 'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton',
'New Mexico': 'Santa Fe', 'New York': 'Albany', 'North Carolina': 'Raleigh',
'North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City',
'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence',
'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee':
'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont':
'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia',
'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}

for quiz_num in range(35):
# TODO: Create the quiz and answer key files.
quiz_file = open(f'capitalsquiz{quiz_num + 1}.txt', 'w', encoding='UTF-8')

answer_file = open(
    f'capitalsquiz_answers{quiz_num + 1}', 'w', encoding='UTF-8')

Now it is creating the right file path

lapis mango
#

See this line?

    quiz_file = open(f'capitalsquiz{quiz_num + 1}.txt', 'w', encoding='UTF-8')

It's not making any use of quizFolder.

#

Stick quizFolder/ after open(

robust rune
#

My god. I was putting the code as ```py
quiz_file = open(f'capitalsquiz{quiz_num + 1}.txt', 'w', encoding='UTF-8')

#

also how do i make my text look cleaner when writing code in discord

lapis mango
#

!code

grand ivyBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

lapis mango
#

So

```py
your code
goes here
```

#

You can go back and edit the previous message to test that.

grand ivyBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.