I have a very elementary question, but I've tried searching past posts and can't seem to find anything that can help. I'm learning about file i/o in Python. All the tutorials I've seen thus far seem to skip a step and just assume that a file has already been created, and just being by saying something like handleName = open('text.txt', 'r'), but this leaves 2 questions unanswered for me:
Do I have to create the file manually and name it? I'm using a Mac, so would I have to go to Applications, open up TextEdit, create and save the file or would I be able to do that through some command in IDLE?
I tried manually creating a file (as described above), but then when I tried entering openfile = open('test_readline', 'r'), I got the error: IOError: [Errno 2] No such file or directory: 'abc'
Regarding the error, I'm assuming I have to declare the path, but how do I do so in Python?
openfile = open('test_readline', 'w')
^^
Opening in write mode will create the file if it doesn't already exist. Now you can write into it and close the file pointer and it will be saved.
To be able to read from any file, the file must exist.Right? Now look here, file I/O has the syntax as shown below:
fp = open('file_name', mode) # fp is a file object
The second argument, i.e mode describes the way in which file will be used. w mode will open any existing file(if it exists) with the name as given in first argument. Otherwise it creates a new file with the same name. Beside, if you are on Windows and want to open a file in binary mode then append b to the mode. Eg. to open file to write in binary mode, use wb. Make a note that if you try to open any existing file in w (writing) mode then the existing file with the same name will be erased. If you want to write to the existing file without getting the old data erased, then use the a mode.It adds the new data at the end of the previous one.
fw = open('file_name','w')
fa = open('file_name','a') # append mode
To know in detail you can refer the doc at
Python File I/O.
I hope this helps!
Python will automatically use the default path.
import os
default_path = os.getcwd() ## Returns the default path
new_path = "C:\\project\\" ## Path directory
os.chdir(path) ## Changes the current directory
Once you change the path, the files you write and read will be in C:\project. If you try and read a project else where, the program will fail.
os.chdir is how you declare or set a path in python.
Do I have to create the file manually and name it?
Do you mean as a user, must you use existing tools to create a file, then return to Python to work on it? No. Python has all the tools necessary to create a file. As already explained by vks in their answer, you must open the file using a mode that will create the file if it doesn't exist. You've chosen read ('r') mode, which will (correctly) throw an error if there is no file to read at the location you've specified, which brings us to...
I'm assuming I have to declare the path, but how do I do so in Python?
If you do not (if you say, e.g., "filename.txt"), Python will look in its current working directory. By default, this is the current working directory of the shell when you invoke the Python interpreter. This is almost always true unless some program has changed it, which is unusual. To specify the path, you can either hardcode it like you're doing to the filename:
open('/full/path/to/filename.txt')
or you can build it using the os.path module.
Example:
I created an empty directory and opened the Python interpreter in it.
>>> with open('test.txt'): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'test.txt'
>>> with open('test.txt', 'w'): pass
...
>>>
As noted, read mode (the default) gives an error because there is no file. Write mode creates a file for us with nothing in it. Now we can see the file in the directory, and opening with read mode works:
>>> os.listdir(os.getcwd())
['test.txt']
>>> with open('test.txt'): pass
...
>>> # ^ No IOError because it exists now
Now I create a subdirectory called 'subdir' and move the text file in there. I did this on the command line but could have just as easily done it in Python:
>>> with open('test.txt'): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'test.txt'
>>> with open('subdir/test.txt'): pass
...
Now we have to specify the relative path (at least) to open the file, just like on the command line. Here I "hardcoded" it but it can just as easily be "built up" using the os module:
>>> with open(os.path.join(os.getcwd(), 'subdir', 'test.txt')): pass
(That is just one way it could be done, as an example.)
Related
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 7 months ago.
I'm trying to write a simple program to read a file and search for a word then print how many times that word is found in the file. Every time I type in "test.rtf" (which is the name of my document) I get this error:
Traceback (most recent call last):
File "/Users/AshleyStallings/Documents/School Work/Computer Programming/Side Projects/How many? (Python).py", line 9, in <module>
fileScan= open(fileName, 'r') #Opens file
FileNotFoundError: [Errno 2] No such file or directory: 'test.rtf'
In class last semester, I remember my professor saying you have to save the file in a specific place? I'm not sure if he really said that though, but I'm running apple OSx if that helps.
Here's the important part of my code:
fileName= input("Please enter the name of the file you'd like to use.")
fileScan= open(fileName, 'r') #Opens file
If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. In your case, the file test.rtf must be in the same directory in which you execute the program.
You are obviously performing programming tasks in Python under Mac OS. There, I recommend to work in the terminal (on the command line), i.e. start the terminal, cd to the directory where your input file is located and start the Python script there using the command
$ python script.py
In order to make this work, the directory containing the python executable must be in the PATH, a so-called environment variable that contains directories that are automatically used for searching executables when you enter a command. You should make use of this, because it simplifies daily work greatly. That way, you can simply cd to the directory containing your Python script file and run it.
In any case, if your Python script file and your data input file are not in the same directory, you always have to specify either a relative path between them or you have to use an absolute path for one of them.
Is test.rtf located in the same directory you're in when you run this?
If not, you'll need to provide the full path to that file.
Suppose it's located in
/Users/AshleyStallings/Documents/School Work/Computer Programming/Side Projects/data
In that case you'd enter
data/test.rtf
as your file name
Or it could be in
/Users/AshleyStallings/Documents/School Work/Computer Programming/some_other_folder
In that case you'd enter
../some_other_folder/test.rtf
As noted above the problem is in specifying the path to your file.
The default path in OS X is your home directory (/Users/macbook represented by ~ in terminal ...you can change or rename the home directory with the advanced options in System Preferences > Users & Groups).
Or you can specify the path from the drive to your file in the filename:
path = "/Users/macbook/Documents/MyPython/"
myFile = path + fileName
You can also catch the File Not Found Error and give another response using try:
try:
with open(filename) as f:
sequences = pick_lines(f)
except FileNotFoundError:
print("File not found. Check the path variable and filename")
exit()
A good start would be validating the input. In other words, you can make sure that the user has indeed typed a correct path for a real existing file, like this:
import os
fileName = input("Please enter the name of the file you'd like to use.")
while not os.path.isfile(fileName):
fileName = input("Whoops! No such file! Please enter the name of the file you'd like to use.")
This is with a little help from the built in module os, That is a part of the Standard Python Library.
You might need to change your path by:
import os
path=os.chdir(str('Here_should_be_the_path_to_your_file')) #This command changes directory
This is what worked for me at least! Hope it works for you too!
Difficult to give code examples in the comments.
To read the words in the file, you can read the contents of the file, which gets you a string - this is what you were doing before, with the read() method - and then use split() to get the individual words.
Split breaks up a String on the delimiter provided, or on whitespace by default. For example,
"the quick brown fox".split()
produces
['the', 'quick', 'brown', 'fox']
Similarly,
fileScan.read().split()
will give you an array of Strings.
Hope that helps!
First check what's your file format(e.g: .txt, .json, .csv etc ),
If your file present in PWD , then just give the name of the file along with the file format inside either single('')or double("") quote and the appropriate operation mode as your requirement
e.g:
with open('test.txt','r') as f: data=f.readlines() for i in data: print(i)
If your file present in other directory, then just give the full path name where is your file is present and file name along with file format of the file inside either single('')or double("") quote and the appropriate operation mode as your requirement.
If it showing unicode error just put either r before quote of file path or else put '/' instead of ''
with open(r'C:\Users\soman\Desktop\test.txt','r') as f: data=f.readlines() for i in data: print(i)
The mistake I did was
my code :
x = open('python.txt')
print(x)
But the problem was in file directory ,I saved it as python.txt instead of just python .
So my file path was
->C:\Users\noob\Desktop\Python\Course 2\python.txt.txt
That is why it was giving a error.
Name your file without .txt it will run.
Im trying to create a random set of files from a list of file names in python 3.
#!/usr/local/bin/python3
import random
import sys
random.seed()
print("Reading lines from ", sys.argv[1])
linecount = 0
lines = []
with open(sys.argv[1]) as f:
for line in f:
lines.append(line)
filelist = random.sample(lines, 5);
for newfile in filelist:
print("Touching file ", newfile)
open(newfile.rstrip(), "a+")
This always fails for the first file with:
$ : ./file-gen.py files.txt
Reading lines from files.txt
Touching file 62/6226320DE.pdf
Traceback (most recent call last):
File "./file-gen.py", line 19, in <module>
open(newfile.rstrip(), "a+");
FileNotFoundError: [Errno 2] No such file or directory: '62/6226320DE.pdf'
Any ideas what's missing?
I believe the problem is that the file mode a+ will create the file if its not present, but not the directory. You would have to write custom code to split the line of the filename from the path (or better still use os.path : https://docs.python.org/2/library/os.path.html, perhaps even os.path.dirname(path)
Have a look at: How to check if a directory exists and create it if necessary?
Be wary of security considerations of creating random paths in your system. Validating that the paths are inside a particular sandbox (consider someone putting an entry in your file of ../../ or /etc/passwd to get you to append random user data.. os.path.abspath can be useful - for this reason I am wary of pasting code which will just create random directories that you copy and paste in without considering that effect.
I would suggest as a first step trying to open a specific file rather than a random set of files from the input to rule out permissions and path problems.
You should also try printing os.path.getcwd() to make sure that you have write permissions there.
I have the following method in Python.
def get_rum_data(file_path, query):
if file_path is not None and query is not None:
command = FETCH_RUM_COMMAND_DATA % (constants.RUM_JAR_PATH,
constants.RUM_SERVER, file_path,
query)
print command
execute_command(command).communicate()
Now inside get_rum_data I need to create the file if it does not exists, if it exists I need to append the data. How to do that in python.
I tried, open(file_path, 'w') , which gave me an exception.
Traceback (most recent call last):
File "utils.py", line 180, in <module>
get_rum_data('/User/rokumar/Desktop/sample.csv', '\'show tables\'')
File "utils.py", line 173, in get_rum_data
open(file_path, 'w')
IOError: [Errno 2] No such file or directory: '/User/rokumar/Desktop/sample.csv'
I though open would create file in write mode.
It shall be as simple as:
fname = "/User/rokumar/Desktop/sample.csv"
with open(fname, "a") as f:
# do here what you want
# it will get closed at this point by context manager
But I would suspect, that you are trying to use not existing directory. Usually, "a" mode creates the file if it can be created.
Make sure, the directory exists.
You can check if all directories in file_path exist prior to trying to write a file.
import os
file_path = '/Users/Foo/Desktop/bar.txt'
print os.path.dirname(file_path)
# /Users/Foo/Desktop
if not os.path.exists(os.path.dirname(file_path)):
os.mkdirs(os.path.dirname(file_path))
# recursively create directories if necessary
with open(file_path, "a") as my_file:
# mode a will either create the file if it does not exist
# or append the content to its end if it exists.
my_file.write(your_text_to_append)
-- Edit: Small and probably unnecessary extension --
expanduser:
In your case, as it turned out that the initial problem was a missing s in the path to the user directory, there is a useful feature for resolving the current users base directory (works for unix, linux and windows): see expanduser from the os.path module. With that you could write your path as path = '~/Desktop/bar.txt' and the tilde (~) will be expanded just like on your shell. (with the additional benefit that if you started your script from another user it would then expand to her home directory.
App config directory:
Since in most cases it is not desirable to write a file to the desktop (*nix systems for instance might not have a desktop installed), there is a great utility function in the click package. If you look at the get_app_dir() function on Github, you can see how they provide expanding to an appropriate app dir and supporting multiple operating systems (the function has no dependencies besides the WIN variable that is defined in the _compat.py module as WIN = sys.platform.startswith('win') and the _posixify() function defined on line 17. Often that's a good starting point for defining an app directory to store certain data in.
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 7 months ago.
I'm trying to write a simple program to read a file and search for a word then print how many times that word is found in the file. Every time I type in "test.rtf" (which is the name of my document) I get this error:
Traceback (most recent call last):
File "/Users/AshleyStallings/Documents/School Work/Computer Programming/Side Projects/How many? (Python).py", line 9, in <module>
fileScan= open(fileName, 'r') #Opens file
FileNotFoundError: [Errno 2] No such file or directory: 'test.rtf'
In class last semester, I remember my professor saying you have to save the file in a specific place? I'm not sure if he really said that though, but I'm running apple OSx if that helps.
Here's the important part of my code:
fileName= input("Please enter the name of the file you'd like to use.")
fileScan= open(fileName, 'r') #Opens file
If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. In your case, the file test.rtf must be in the same directory in which you execute the program.
You are obviously performing programming tasks in Python under Mac OS. There, I recommend to work in the terminal (on the command line), i.e. start the terminal, cd to the directory where your input file is located and start the Python script there using the command
$ python script.py
In order to make this work, the directory containing the python executable must be in the PATH, a so-called environment variable that contains directories that are automatically used for searching executables when you enter a command. You should make use of this, because it simplifies daily work greatly. That way, you can simply cd to the directory containing your Python script file and run it.
In any case, if your Python script file and your data input file are not in the same directory, you always have to specify either a relative path between them or you have to use an absolute path for one of them.
Is test.rtf located in the same directory you're in when you run this?
If not, you'll need to provide the full path to that file.
Suppose it's located in
/Users/AshleyStallings/Documents/School Work/Computer Programming/Side Projects/data
In that case you'd enter
data/test.rtf
as your file name
Or it could be in
/Users/AshleyStallings/Documents/School Work/Computer Programming/some_other_folder
In that case you'd enter
../some_other_folder/test.rtf
As noted above the problem is in specifying the path to your file.
The default path in OS X is your home directory (/Users/macbook represented by ~ in terminal ...you can change or rename the home directory with the advanced options in System Preferences > Users & Groups).
Or you can specify the path from the drive to your file in the filename:
path = "/Users/macbook/Documents/MyPython/"
myFile = path + fileName
You can also catch the File Not Found Error and give another response using try:
try:
with open(filename) as f:
sequences = pick_lines(f)
except FileNotFoundError:
print("File not found. Check the path variable and filename")
exit()
A good start would be validating the input. In other words, you can make sure that the user has indeed typed a correct path for a real existing file, like this:
import os
fileName = input("Please enter the name of the file you'd like to use.")
while not os.path.isfile(fileName):
fileName = input("Whoops! No such file! Please enter the name of the file you'd like to use.")
This is with a little help from the built in module os, That is a part of the Standard Python Library.
You might need to change your path by:
import os
path=os.chdir(str('Here_should_be_the_path_to_your_file')) #This command changes directory
This is what worked for me at least! Hope it works for you too!
Difficult to give code examples in the comments.
To read the words in the file, you can read the contents of the file, which gets you a string - this is what you were doing before, with the read() method - and then use split() to get the individual words.
Split breaks up a String on the delimiter provided, or on whitespace by default. For example,
"the quick brown fox".split()
produces
['the', 'quick', 'brown', 'fox']
Similarly,
fileScan.read().split()
will give you an array of Strings.
Hope that helps!
First check what's your file format(e.g: .txt, .json, .csv etc ),
If your file present in PWD , then just give the name of the file along with the file format inside either single('')or double("") quote and the appropriate operation mode as your requirement
e.g:
with open('test.txt','r') as f: data=f.readlines() for i in data: print(i)
If your file present in other directory, then just give the full path name where is your file is present and file name along with file format of the file inside either single('')or double("") quote and the appropriate operation mode as your requirement.
If it showing unicode error just put either r before quote of file path or else put '/' instead of ''
with open(r'C:\Users\soman\Desktop\test.txt','r') as f: data=f.readlines() for i in data: print(i)
The mistake I did was
my code :
x = open('python.txt')
print(x)
But the problem was in file directory ,I saved it as python.txt instead of just python .
So my file path was
->C:\Users\noob\Desktop\Python\Course 2\python.txt.txt
That is why it was giving a error.
Name your file without .txt it will run.
I am trying to replicate IDLE's alt + m command (open a module in the sys path) in Notepad++. I like Notepad++ for editing (rather than IDLE), but this is one feature I cannot find.
When alt+m is pressed, I want it to run a program that asks for a module (that's fairly straightforward, so I can do that). My problem is finding the module and then opening it in Notepad++, not simply running the program. In addition, I want it to open in the same instance (same window) in Notepad++, not a new instance.
I've tried this:
import os
f = r"D:\my_stuff\Google Drive\Modules\nums.py"
os.startfile(f, 'notepad++.exe')
However, I get this error:
Traceback (most recent call last):
File '_filePath_', line 3, in <module>
os.startfile(f, 'notepad++.exe')
OSError: [WinError 1155] No application is associated with the specified file for this operation: 'D:\\my_stuff\\Google Drive\\Modules\\nums.py'
How can I fix this?
Also, given a string, such as 'nums.py', how can I find the full path of it? It's going to be in one of two folders: 'D:\\my_stuff\\Google Drive\\Modules' or 'C:\\Python27\Lib' (it could also be in various subfolders in the 'Lib' folder). Alternatively, I could simply do:
try:
fullPath = r'D:\\my_stuff\\Google Drive\\Modules\\' + f
# method of opening file in Notepad++
except (IOError, FileNotFoundError):
fullPath = r'C:\\Python27\\Lib\\' + f
# open in Notepad++
This doesn't account for subfolders and seems rather clunky. Thanks!
If your .py files will be associated w/ notepad++ the os.startfile(f, 'notepad++.exe') will work for you (see ftype).
Unless, you would like to create this association , the following code will open notepad ++ for you:
import subprocess
subprocess.call([r"c:\Program ...Notepad++.exe", r"D:\my_stuff\Google Drive\Modules\nums.py"])
Reference: subprocess.call()