Chloé
Chloé

Reputation: 31

Can't find my file with with open(filename, 'w') on Windows

I'm studying python at school and I would like to test some scripts. However, the computers at school use Linux but at home I use Windows. I find my solution to practice at home but at least, my only problem is when I use with open to create a file in Python.

The program works fine because I verify the correction at school but I can't find my file on my computer.

The program I work on is to clean a text without space or "#" so I just put the list lines here :

with open(deck, 'r') as data: 
    with open('output.txt', 'w') as output: 
        for line in data: 
            if (not ligne_commentee(line)) and (not ligne_vide(line)):
                output.write(supprime_caracteres_commentes(line))

PS : I'm french, so the names of the file are in french...

Deck is deck = "C:\Users\CHLOE\Desktop\mes\texte_test.txt".

This is how I can read my file but I don't find a solution to find the find I create with output.write().

Can you help me ? Thank you !

Upvotes: 3

Views: 589

Answers (1)

r.ook
r.ook

Reputation: 13858

Since you didn't specify an absolute path for output like you did with deck, Python creates output.txt based on your current directory (which will always differ based on where you are running the script).

Put in an absolute path for output.txt:

with open('C:\\Users\\CHLOE\\Desktop\\mes\\output.txt', 'w') as output: 

# ... rest of your code

If output.txt is always relative to your deck input, you can also do this:

import os

output_path = os.path.dirname(deck)
with open(deck, 'r') as data: 
    with open(os.path.join(output_path, 'output.txt'), 'w') as output:

# ... rest of your code

Upvotes: 1

Related Questions