Python Forum
Question: Paths and writing to a file
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question: Paths and writing to a file
#1
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
Reply
#2
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')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Ah thank you, everything worked now. Thank you for your answer. I extremely appreciate it.
Reply
#6
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'
Reply
#7
(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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 397 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 626 Oct-29-2023, 12:40 PM
Last Post: Mark17
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  pdf2image, poppler and paths jehoshua 18 13,938 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 1,416 May-29-2022, 09:11 AM
Last Post: snippsat
  Writing to External File DaveG 9 2,406 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  automatically get absolute paths oclmedyb 1 2,061 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  Writing to file ends incorrectly project_science 4 2,641 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,653 Nov-15-2020, 05:41 PM
Last Post: ateestructural

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020