Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing files via python
#1
Ok so I'm getting an error when I am trying to access a file on my computer. I'm using the python shell and version is 3.6.1 All I want to do with the file is be able to read and write to it. The file I am trying to access is under the folder "My documents". I'm on Windows OS. My code is as follows:

 newfile = open("testfile2", "r") 
The error I am getting is as follows:

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
newfile = open("testfile2", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'testfile2'


I don't understand why I am getting this error as the name of the file is not mispelled in the code. If you can provide help could you please explain what I was doing wrong and what I should do to fix it. :)
Reply
#2
Quote:The file I am trying to access is under the folder "My documents". I'm on Windows OS.
You documents folder is locate at path C:\Users\<username>\Documents
Let say i has a file foo.txt in Documents folder.
Learn to cmd and navigate cd to the file.
Command line use is basic knowledge all programmers most know.
with open('C:/Users/tom/Documents/foo.txt') as f:
   print(f.read())
Output:
hello world
Quote:I'm using the python shell and version is 3.6.1
If doing it from shell,i guess you use not so good IDLE.
You most also give path to file or change to folder with eg using os module.
>>> import os
>>> os.getcwd()
'C:\\Python36'
>>> import os
>>> os.getcwd()
'C:\\Python36'
>>> # Now are we in python36 folder
>>> os.chdir('C:/Users/tom/Documents/')
>>> os.getcwd()
'C:\\Users\\tom\\Documents'
>>> # Now in folder Documents
>>> # Then can just read foo.txt
>>> newfile = open("foo.txt", "r")
>>> newfile.read()
'hello world'
>>> 
You could also look at my tutorial part-1 and part-2.
because i guess all this in new.
Reply
#3
It's hardly possible that on a Windows OS a file could be called just 'testfile2' without any extension. As I remember this OS adds the extension automatically according to the application created the file. Use the full name of the file.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Jul-02-2017, 05:52 PM)wavic Wrote: As I remember this OS adds the extension automatically according to the application created the file.

The Windows operating system doesn't do that. Applications tend to do that in the save as dialog, but it can be deleted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Jul-02-2017, 03:42 PM)gahdzilla Wrote: I'm using the python shell and version is 3.6.1
The clue is here wavic,when he open shell he is in C:\Python36 folder,
and his file testfile2 with our without extension is in C:\Users\<username>\Documents

You can be right that he manged to get to right folder in shell and have forget extension eg testfile2.txt,
but i will be very surprised if this is the answer,based on his explanation.

As mention be @ichabod801 applications tend to make file extension in Windows.
But save bar. and choose All files in notepad or just All files in IDLE.
Both will save file with no extension.
Python do of course read it fine without extension.
with open('C:/Users/tom/Documents/bar') as f:
   print(f.read())
Output:
hello world
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing same python script from multiple computers bigrockcrasher 1 1,711 May-25-2022, 08:35 PM
Last Post: Gribouillis
  Python modules for accessing the configuration of relevant paths Imago 1 1,385 May-07-2022, 07:28 PM
Last Post: Larz60+
  List index out of range error while accessing 2 lists in python K11 2 2,135 Sep-29-2020, 05:24 AM
Last Post: K11
  Accessing IP Cam Audio In Python Haselsmasher 1 5,476 Jul-10-2020, 02:59 AM
Last Post: kiliantics
  Accessing files in various directories and folders ccuny 2 2,175 May-08-2019, 12:11 PM
Last Post: ccuny
  Accessing Files In A Folder asilverg 1 1,854 Feb-15-2019, 08:08 AM
Last Post: buran
  Accessing Files via Mobaxterm Omer123 1 2,880 Jan-23-2019, 12:33 AM
Last Post: micseydel
  Accessing IBM MQ using Python Rajeshs16 6 7,811 Oct-22-2018, 02:39 PM
Last Post: Larz60+
  Running a python tool transforming xml files into epub files silfer 7 5,431 May-10-2018, 03:49 PM
Last Post: snippsat
  Accessing nested dictionary values. Plistlib, Python 2.7 williamlombard 32 20,903 Sep-29-2017, 06:46 AM
Last Post: williamlombard

Forum Jump:

User Panel Messages

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