Bottom Page

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
Quote
#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
Quote
#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.
Quote
#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
Quote
#5
Ah thank you, everything worked now. Thank you for your answer. I extremely appreciate it.
Quote
#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'
wavic likes this post
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help|CSV Writing on List with Inner Commas soothsayerpg 1 49 7 minutes ago
Last Post: soothsayerpg
  Writing the Path to a file properly dcw9996 2 132 Jul-01-2019, 11:26 PM
Last Post: dcw9996
  Shortest paths to win snake and ladder sandaab 5 308 Jun-30-2019, 03:20 PM
Last Post: sandaab
  Avoiding empty line in writing process csv file go127a 4 267 May-10-2019, 01:33 PM
Last Post: go127a
  csv to xlsx, remove " while writing to excel anna 3 253 Apr-16-2019, 07:57 AM
Last Post: anna
  writing in particular positions clarablanes 5 282 Apr-13-2019, 04:07 PM
Last Post: clarablanes
  Trouble writing over serial EngineerNeil 1 234 Apr-07-2019, 08:17 PM
Last Post: j.crater
  Writing integer to file jpezz 4 408 Apr-02-2019, 07:22 PM
Last Post: jpezz
  Writing to Excel file aa8888 2 237 Mar-06-2019, 06:04 PM
Last Post: aa8888
  Who converts data when writing to a database with an encoding different from utf8? AlekseyPython 1 232 Mar-04-2019, 08:26 AM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)