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
#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


Messages In This Thread
Question: Paths and writing to a file - by mwmaw - Dec-20-2016, 08:12 AM
RE: Question: Paths and writing to a file - by mwmaw - Dec-20-2016, 08:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 571 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 787 Oct-29-2023, 12:40 PM
Last Post: Mark17
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,417 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,044 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  pdf2image, poppler and paths jehoshua 18 15,105 Jun-14-2022, 06:38 AM
Last Post: jehoshua
  Windows paths issue otalado 3 1,518 May-29-2022, 09:11 AM
Last Post: snippsat
  Writing to External File DaveG 9 2,577 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  automatically get absolute paths oclmedyb 1 2,143 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  Writing to file ends incorrectly project_science 4 2,748 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,816 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