Python Forum

Full Version: Question: Paths and writing to a file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First off, hello! I've been trying to find a good forum to post questions to and, when I'm better with python, hopefully I can help people out with their questions as well!


I'll try my best to explain my situation.

The Problem:  
    my_variable = "<html><head><title>Look At This</title>"
my_html_file = open("/Projects/my_html_file.html", "w")
 my_html_file.write(my_variable)                                        
Not able to write (create new) "my_html_file.html" by using "w".
                                                                                                            to my understanding, "w" is supposed to create the file, in the event that that file is not present.


Question

    1.) I have been wanting to get into python for quite sometime, but it seems that there is always an issue with pathing. Currently, my python install path is: C:\Python35-32.

     Inside that folder, there is a folder named "Projects." System path: C:\Python35-32\Projects (I am aware there is not a subfolder for the specific project I am working          with. To keep it simple, I'm just trying to create an html file output into "Projects."

    Is the path in the code correct in order to execute this code correctly? How would I write that code in order to execute what I want?

    Just in case my environment variables are off, I can send you an image displaying them. Due to being a new member, I cannot post images.

I hope that this is enough information to help a tiny bit. I guess a few meta details would be:

OS: Windows 10
Python: version 3.5



Obviously if any other information is required to help out, please let me know. Thank you for taking the time to read this.

-mwmaw
Hello!  
Why don't you use a full path but relative? If you do not want to you can get the full path to a file with os.path.abspath('filename')
Second, what do you mean "create a new file"? You can't create a new file if it has the same name as the old one. Or "new" means that the old content is vanish. If that is what you are telling us, it's normal if you use 'w'. To append to a file 'a' is what you need. 


file_obj = open('file_name.txt', 'a')
Quote:Why don't you use a full path but relative?


My apologies, I'm quite new to programming. I've just been following tutorials with a certain YouTuber (who seems to know what he's doing) And I couldn't find out why, when I copied his exact code, I did not get the same result (it just flat out didn't work). Which led me to believe that perhaps, something was wrong with my path. 

Now when you use 

file_obj = open('file_name.txt', 'a')

From what I understand from your post, this allows you to open files from anywhere within the working directory? It seems like when I go to actually write the file (there is no file created yet) it does not create it, with the code that I am using in the original post.

Perhaps I should take a step back from trying to understand this, but when I delve into trying to learn, it's always in the back of my head that I don't understand directories or how to navigate to files within python.

For example: With my current python install directory, a few sources say that from within CMD, I should be able to just type python example.py, no matter where the current directory is that I'm currently inside of (within cmd). This is not the case. It always gives me 

C:\Users\Aaron>python test1.py
python: can't open file 'test1.py': [Errno 2] No such file or directory

when in fact, test1.py exists inside my scripts subfolder of Python35-32 (Main directory) . Do I have a fundamental misunderstanding about how this concept works? The help that I read on this specifically stated that I should be able to run any .py file, regardless of where I have it, as long as the main "Python35-32" directory is above everything else.

Thank you for your response, by the way. I really want to understand this.
I read you and I think you have to understand what is absolute path and relative one. Correct me if I'm wrong. 
If there is a file named 'echo_server.py' which is located in D:\archives\ and you currently are in C:\ you can't just type python echo_server.py because it will look at C:\ for echo_server.py and all paths in PATH environment variable. The way to run it is to provide the full path to the file or add it to PATH. 
C:\ > python D:\archive\echo_sever.py

(Dec-20-2016, 09:20 AM)wavic Wrote: [ -> ]I read you and I think you have to understand what is absolute path and relative one. Correct me if I'm wrong. If there is a file named 'echo_server.py' which is located in D:\archives\ and you currently are in C:\ you can't just type python echo_server.py because it will look at C:\ for echo_server.py and all paths in PATH environment variable. The way to run it is to provide the full path to the file or add it to PATH.
 C:\ > python D:\archive\echo_sever.py 
(Dec-20-2016, 08:42 AM)mwmaw Wrote: [ -> ]From what I understand from your post, this allows you to open files from anywhere within the working directory? It seems like when I go to actually write the file (there is no file created yet) it does not create it, with the code that I am using in the original post.
It will create the file into the working directory if it not exists. It it does, 'w' is going to overwrite its contend.
If you run your script from C:\, created file will be located in C:\. In order to create a file somewhere else you have to provide the path to that location.
file_obj = open('C:\projects\Dec-20-16\my_file.txt', 'w')
For example
Ah thank you, everything worked now. Thank you for your answer. I extremely appreciate it.
And remember never a single backslash this way \.
You are lucky here because not hitting any escape character.
Eg with an other file name it had gone wrong.
>>> s = 'C:\projects\Dec-20-16\test.txt'
>>> print(s)
C:\projects\Dec-20-16    est.txt
So always \\\ or / or r'path'
(Dec-20-2016, 03:15 PM)snippsat Wrote: [ -> ]And remember never a single backslash this way \.
You are lucky here because not hitting any escape character.
Eg with an other file name it had gone wrong.
>>> s = 'C:\projects\Dec-20-16\test.txt'
>>> print(s)
C:\projects\Dec-20-16    est.txt
So always \\\ or / or r'path'

Yes, I have missed that. os.path module can handle paths pretty well.
https://docs.python.org/3/library/os.path.html