Opening/running Excel file from python - python

I need to start excel and open a file directly from python. Currently I am using:
import os
os.system('start excel.exe file.xls')
However I do not get desired result. I want to open a file from local destination (file is in the same folder with program), but this code open's the file with same name in my home (user) directory and not from my program directory.

The problem is that the directory where the program is located is not used. The current working directory is. So you have to find out which directory your program is located in, which python conveniently prepared for you in:
sys.path[0]
and either change directory to it:
os.chdir(sys.path[0])
or give full path for the file you want to open
os.system('start excel.exe "%s\\file.xls"' % (sys.path[0], ))
Note, that while Windows generally accept forward slash as directory separator, the command shell (cmd.exe) does not, so backslash has to be used here. start is Windows-specific, so it's not big problem to hardcode it here. More importantly note that Windows don't allow " in file-names, so the quoting here is actually going to work (quoting is needed, because the path is quite likely to contain space on Windows), but it's bad idea to quote like this in general!

You can also define the directory, where the python should operate.
import os
os.chdir('C:\\my_folder\\subfolder')
os.system('start excel.exe my_workbook.xlsx')
Don't forget to use backslashes in your path and there must be two of them everytime.
my_workbook.xlxs - here will be the name of your file
That file must be in that folder :)

Related

Importing a txt file into jupyter [duplicate]

I am trying to open the file recentlyUpdated.yaml from my Python script. But when I try using:
open('recentlyUpdated.yaml')
I get an error that says:
IOError: [Errno 2] No such file or directory: 'recentlyUpdated.yaml'
Why? How can I fix the problem?
Ensure the file exists (and has the right file extension): use os.listdir() to see the list of files in the current working directory.
Ensure you're in the expected directory using os.getcwd().
(If you launch your code from an IDE, you may be in a different directory.)
You can then either:
Call os.chdir(dir) where dir is the directory containing the file. Then, open the file using just its name, e.g. open("file.txt").
Specify an absolute path to the file in your open call.
Use a raw string (r"") if your path uses backslashes, like
so: dir = r'C:\Python32'
If you don't use raw string, you have to escape every backslash: 'C:\\User\\Bob\\...'
Forward-slashes also work on Windows 'C:/Python32' and do not need to be escaped.
Let me clarify how Python finds files:
An absolute path is a path that starts with your computer's root directory, for example C:\Python\scripts if you're on Windows.
A relative path is a path that does not start with your computer's root directory, and is instead relative to something called the working directory. You can view Python's current working directory by calling os.getcwd().
If you try to do open('sortedLists.yaml'), Python will see that you are passing it a relative path, so it will search for the file inside the current working directory.
Calling os.chdir() will change the current working directory.
Example: Let's say file.txt is found in C:\Folder.
To open it, you can do:
os.chdir(r'C:\Folder')
open('file.txt') # relative path, looks inside the current working directory
or
open(r'C:\Folder\file.txt') # absolute path
Most likely, the problem is that you're using a relative file path to open the file, but the current working directory isn't set to what you think it is.
It's a common misconception that relative paths are relative to the location of the python script, but this is untrue. Relative file paths are always relative to the current working directory, and the current working directory doesn't have to be the location of your python script.
You have three options:
Use an absolute path to open the file:
file = open(r'C:\path\to\your\file.yaml')
Generate the path to the file relative to your python script:
from pathlib import Path
script_location = Path(__file__).absolute().parent
file_location = script_location / 'file.yaml'
file = file_location.open()
(See also: How do I get the path and name of the file that is currently executing?)
Change the current working directory before opening the file:
import os
os.chdir(r'C:\path\to\your\file')
file = open('file.yaml')
Other common mistakes that could cause a "file not found" error include:
Accidentally using escape sequences in a file path:
path = 'C:\Users\newton\file.yaml'
# Incorrect! The '\n' in 'Users\newton' is a line break character!
To avoid making this mistake, remember to use raw string literals for file paths:
path = r'C:\Users\newton\file.yaml'
# Correct!
(See also: Windows path in Python)
Forgetting that Windows doesn't display file extensions:
Since Windows doesn't display known file extensions, sometimes when you think your file is named file.yaml, it's actually named file.yaml.yaml. Double-check your file's extension.
The file may be existing but may have a different path. Try writing the absolute path for the file.
Try os.listdir() function to check that atleast python sees the file.
Try it like this:
file1 = open(r'Drive:\Dir\recentlyUpdated.yaml')
Possibly, you closed the 'file1'.
Just use 'w' flag, that create new file:
file1 = open('recentlyUpdated.yaml', 'w')
mode is an optional string that specifies the mode in which the file
is opened. It defaults to 'r' which means open for reading in text
mode. Other common values are 'w' for writing (truncating the file if
it already exists)...
(see also https://docs.python.org/3/library/functions.html?highlight=open#open)
If is VSCode see the workspace. If you are in other workspace this error can rise
Understanding absolute and relative paths
The term path means exactly what it sounds like. It shows the steps that need to be taken, into and out of folders, to find a file. Each step on the path is either a folder name, the special name . (which means the current folder), or the special name .. (which means to go back/out into the parent folder).
The terms absolute and relative also have their usual English meaning. A relative path shows where something is relative to some start point; an absolute path is a location starting from the top.
Paths that start with a path separator, or a drive letter followed by a path separator (like C:/foo) on Windows, are absolute. (On Windows there are also UNC paths, which are necessarily absolute. Most people will never have to worry about these.)
Paths that directly start with a file or folder name, or a drive letter followed directly by the file or folder name (like C:foo) on Windows, are relative.
Understanding the "current working directory"
Relative paths are "relative to" the so-called current working directory (hereafter abbreviated CWD). At the command line, Linux and Mac use a common CWD across all drives. (The entire file system has a common "root", and may include multiple physical storage devices.) Windows is a bit different: it remembers the most recent CWD for each drive, and has separate functionality to switch between drives, restoring those old CWD values.
Each process (this includes terminal/command windows) has its own CWD. When a program is started from the command line, it will get the CWD that the terminal/command process was using. When a program is started from a GUI (by double-clicking a script, or dragging something onto the script, or dragging the script onto a Python executable) or by using an IDE, the CWD might be any number of things depending on the details.
Importantly, the CWD is not necessarily where the script is located.
The script's CWD can be checked using os.getcwd, and modified using os.chdir. Each IDE has its own rules that control the initial CWD; check the documentation for details.
To set the CWD to the folder that contains the current script, determine that path and then set it:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Verifying the actual file name and path
There are many reasons why the path to a file might not match expectations. For example, sometimes people expect C:/foo.txt on Windows to mean "the file named foo.txt on the desktop". This is wrong. That file is actually - normally - at C:/Users/name/Desktop/foo.txt (replacing name with the current user's username). It could instead be elsewhere, if Windows is configured to put it elsewhere. To find the path to the desktop in a portable way, see How to get Desktop location?.
It's also common to mis-count ..s in a relative path, or inappropriately repeat a folder name in a path. Take special care when constructing a path programmatically. Finally, keep in mind that .. will have no effect while already in a root directory (/ on Linux or Mac, or a drive root on Windows).
Take even more special care when constructing a path based on user input. If the input is not sanitized, bad things could happen (e.g. allowing the user to unzip a file into a folder where it will overwrite something important, or where the user ought not be allowed to write files).
Another common gotcha is that the special ~ shortcut for the current user's home directory does not work in an absolute path specified in a Python program. That part of the path must be explicitly converted to the actual path, using os.path.expanduser. See Why am I forced to os.path.expanduser in python? and os.makedirs doesn't understand "~" in my path.
Keep in mind that os.listdir will give only the file names, not paths. Trying to iterate over a directory listed this way will only work if that directory is the current working directory.
It's also important to make sure of the actual file name. Windows has an option to hide file name extensions in the GUI. If you see foo.txt in a window, it could be that the file's actual name is foo.txt.txt, or something else. You can disable this option in your settings. You can also verify the file name using the command line; dir will tell you the truth about what is in the folder. (The Linux/Mac equivalent is ls, of course; but the problem should not arise there in the first place.)
Backslashes in ordinary strings are escape sequences. This causes problems when trying to a backslash as the path separator on Windows. However, using backslashes for this is not necessary, and generally not advisable. See Windows path in Python.
When trying to create a new file using a file mode like w, the path to the new file still needs to exist - i.e., all the intervening folders. See for example Trying to use open(filename, 'w' ) gives IOError: [Errno 2] No such file or directory if directory doesn't exist. Also keep in mind that the new file name has to be valid. In particular, it will not work to try to insert a date in MM/DD/YYYY format into the file name, because the /s will be treated as path separators.
Check the path that has been mentioned, if it's absolute or relative.
If its something like-->/folder/subfolder/file -->Computer will search for folder in root directory.
If its something like--> ./folder/subfolder/file --> Computer will search for folder in current working directory.
If you are using IDE like VScode, make sure you have opened the IDE from the same directory where you have kept the file you want to access.
For example, if you want to access file.txt which is inside the Document, try opening the IDE from Document by right clicking in the directory and clicking "Open with "

open file on server with notepad via windows cmd in python

I am trying to opne a file that is located on a server using the windows cmd. What I do is basically the following thing:
import os
os.system('pushd '+ \\Server\PathToFile)
os.system('start Notepad '+ NameOfFile)
The point is that this works if I enter it by hand in the cmd. If I try to do it within python it does not work. I get this error message:
CMD.EXE was started with the path given above as current directory.
UNC-paths are not supported, therefore the windows-directory is used as
current directory.
The actual error message is in german, that's why I translated it and I'm not sure whether it's understandable or not. What actually happens is that the path where notepad is looking for the current file is C:\Windows instead of the path that I indicated.
Windows doesn't support setting the current directory to an UNC path, and it wouldn't have worked anyway since those are 2 separate os.system commands.
You could mount a drive on this path and use os.chdir, but that would make it more complex yet!
You don't really need current directory change. Moreover os.system is deprecated, it's recommended to use subprocess instead.
So change your code to run the command providing full path to the file:
import subprocess
subprocess.call(["start","notepad",os.path.join("\\Server\PathToFile",NameOfFile)],shell=True)
but I suspect you'd be better off with
os.startfile(os.path.join("\\Server\PathToFile",NameOfFile))
(default associations of Windows will probably open "notepad" in background, that's one-line & simple, & users can even change the editor used by just changing text file association in windows)

Python | IOError Directory File Not Found [duplicate]

I am trying to open the file recentlyUpdated.yaml from my Python script. But when I try using:
open('recentlyUpdated.yaml')
I get an error that says:
IOError: [Errno 2] No such file or directory: 'recentlyUpdated.yaml'
Why? How can I fix the problem?
Ensure the file exists (and has the right file extension): use os.listdir() to see the list of files in the current working directory.
Ensure you're in the expected directory using os.getcwd().
(If you launch your code from an IDE, you may be in a different directory.)
You can then either:
Call os.chdir(dir) where dir is the directory containing the file. Then, open the file using just its name, e.g. open("file.txt").
Specify an absolute path to the file in your open call.
Use a raw string (r"") if your path uses backslashes, like
so: dir = r'C:\Python32'
If you don't use raw string, you have to escape every backslash: 'C:\\User\\Bob\\...'
Forward-slashes also work on Windows 'C:/Python32' and do not need to be escaped.
Let me clarify how Python finds files:
An absolute path is a path that starts with your computer's root directory, for example C:\Python\scripts if you're on Windows.
A relative path is a path that does not start with your computer's root directory, and is instead relative to something called the working directory. You can view Python's current working directory by calling os.getcwd().
If you try to do open('sortedLists.yaml'), Python will see that you are passing it a relative path, so it will search for the file inside the current working directory.
Calling os.chdir() will change the current working directory.
Example: Let's say file.txt is found in C:\Folder.
To open it, you can do:
os.chdir(r'C:\Folder')
open('file.txt') # relative path, looks inside the current working directory
or
open(r'C:\Folder\file.txt') # absolute path
Most likely, the problem is that you're using a relative file path to open the file, but the current working directory isn't set to what you think it is.
It's a common misconception that relative paths are relative to the location of the python script, but this is untrue. Relative file paths are always relative to the current working directory, and the current working directory doesn't have to be the location of your python script.
You have three options:
Use an absolute path to open the file:
file = open(r'C:\path\to\your\file.yaml')
Generate the path to the file relative to your python script:
from pathlib import Path
script_location = Path(__file__).absolute().parent
file_location = script_location / 'file.yaml'
file = file_location.open()
(See also: How do I get the path and name of the file that is currently executing?)
Change the current working directory before opening the file:
import os
os.chdir(r'C:\path\to\your\file')
file = open('file.yaml')
Other common mistakes that could cause a "file not found" error include:
Accidentally using escape sequences in a file path:
path = 'C:\Users\newton\file.yaml'
# Incorrect! The '\n' in 'Users\newton' is a line break character!
To avoid making this mistake, remember to use raw string literals for file paths:
path = r'C:\Users\newton\file.yaml'
# Correct!
(See also: Windows path in Python)
Forgetting that Windows doesn't display file extensions:
Since Windows doesn't display known file extensions, sometimes when you think your file is named file.yaml, it's actually named file.yaml.yaml. Double-check your file's extension.
The file may be existing but may have a different path. Try writing the absolute path for the file.
Try os.listdir() function to check that atleast python sees the file.
Try it like this:
file1 = open(r'Drive:\Dir\recentlyUpdated.yaml')
Possibly, you closed the 'file1'.
Just use 'w' flag, that create new file:
file1 = open('recentlyUpdated.yaml', 'w')
mode is an optional string that specifies the mode in which the file
is opened. It defaults to 'r' which means open for reading in text
mode. Other common values are 'w' for writing (truncating the file if
it already exists)...
(see also https://docs.python.org/3/library/functions.html?highlight=open#open)
If is VSCode see the workspace. If you are in other workspace this error can rise
Understanding absolute and relative paths
The term path means exactly what it sounds like. It shows the steps that need to be taken, into and out of folders, to find a file. Each step on the path is either a folder name, the special name . (which means the current folder), or the special name .. (which means to go back/out into the parent folder).
The terms absolute and relative also have their usual English meaning. A relative path shows where something is relative to some start point; an absolute path is a location starting from the top.
Paths that start with a path separator, or a drive letter followed by a path separator (like C:/foo) on Windows, are absolute. (On Windows there are also UNC paths, which are necessarily absolute. Most people will never have to worry about these.)
Paths that directly start with a file or folder name, or a drive letter followed directly by the file or folder name (like C:foo) on Windows, are relative.
Understanding the "current working directory"
Relative paths are "relative to" the so-called current working directory (hereafter abbreviated CWD). At the command line, Linux and Mac use a common CWD across all drives. (The entire file system has a common "root", and may include multiple physical storage devices.) Windows is a bit different: it remembers the most recent CWD for each drive, and has separate functionality to switch between drives, restoring those old CWD values.
Each process (this includes terminal/command windows) has its own CWD. When a program is started from the command line, it will get the CWD that the terminal/command process was using. When a program is started from a GUI (by double-clicking a script, or dragging something onto the script, or dragging the script onto a Python executable) or by using an IDE, the CWD might be any number of things depending on the details.
Importantly, the CWD is not necessarily where the script is located.
The script's CWD can be checked using os.getcwd, and modified using os.chdir. Each IDE has its own rules that control the initial CWD; check the documentation for details.
To set the CWD to the folder that contains the current script, determine that path and then set it:
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Verifying the actual file name and path
There are many reasons why the path to a file might not match expectations. For example, sometimes people expect C:/foo.txt on Windows to mean "the file named foo.txt on the desktop". This is wrong. That file is actually - normally - at C:/Users/name/Desktop/foo.txt (replacing name with the current user's username). It could instead be elsewhere, if Windows is configured to put it elsewhere. To find the path to the desktop in a portable way, see How to get Desktop location?.
It's also common to mis-count ..s in a relative path, or inappropriately repeat a folder name in a path. Take special care when constructing a path programmatically. Finally, keep in mind that .. will have no effect while already in a root directory (/ on Linux or Mac, or a drive root on Windows).
Take even more special care when constructing a path based on user input. If the input is not sanitized, bad things could happen (e.g. allowing the user to unzip a file into a folder where it will overwrite something important, or where the user ought not be allowed to write files).
Another common gotcha is that the special ~ shortcut for the current user's home directory does not work in an absolute path specified in a Python program. That part of the path must be explicitly converted to the actual path, using os.path.expanduser. See Why am I forced to os.path.expanduser in python? and os.makedirs doesn't understand "~" in my path.
Keep in mind that os.listdir will give only the file names, not paths. Trying to iterate over a directory listed this way will only work if that directory is the current working directory.
It's also important to make sure of the actual file name. Windows has an option to hide file name extensions in the GUI. If you see foo.txt in a window, it could be that the file's actual name is foo.txt.txt, or something else. You can disable this option in your settings. You can also verify the file name using the command line; dir will tell you the truth about what is in the folder. (The Linux/Mac equivalent is ls, of course; but the problem should not arise there in the first place.)
Backslashes in ordinary strings are escape sequences. This causes problems when trying to a backslash as the path separator on Windows. However, using backslashes for this is not necessary, and generally not advisable. See Windows path in Python.
When trying to create a new file using a file mode like w, the path to the new file still needs to exist - i.e., all the intervening folders. See for example Trying to use open(filename, 'w' ) gives IOError: [Errno 2] No such file or directory if directory doesn't exist. Also keep in mind that the new file name has to be valid. In particular, it will not work to try to insert a date in MM/DD/YYYY format into the file name, because the /s will be treated as path separators.
Check the path that has been mentioned, if it's absolute or relative.
If its something like-->/folder/subfolder/file -->Computer will search for folder in root directory.
If its something like--> ./folder/subfolder/file --> Computer will search for folder in current working directory.
If you are using IDE like VScode, make sure you have opened the IDE from the same directory where you have kept the file you want to access.
For example, if you want to access file.txt which is inside the Document, try opening the IDE from Document by right clicking in the directory and clicking "Open with "

How to handle filepaths?

I discovered that a script's "current working directory" is, initially, not the where the script is located, but rather where the user is when he/she runs the script.
If the script is at /Desktop/Projects/pythonProject/myscript.py, but I'm at /Documents/Arbitrary in my terminal when I run the script, then that's going to be it's present working directory, and an attempt at open('data.txt') is going to give File Not Found because it's not looking in the right directory.
So how is a script supposed to open files if it can't know where it's being run from? How is this handled?
My initial thought was to use absolute paths. Say my script needs to open data.txt which is stored alongside it in its package pythonProject. Then I would just say open('/Desktop/Projects/pythonProject/data.txt').
But then you can't ever move the project without editing every path in it, so this can't be the right solution.
Or is the answer simply that you must be in the directory where the script is located whenever you run the script? That doesn't seem right either.
Is there some simple manipulation for this that I'm not thinking of? Are you just supposed to os.chdir to the script's location at the beginning of the script?
Get the current file's directory path, using os.path.dirname, os.path.abspath, os.path.realpath, and the __file__ variable:
import os
file_dir = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
Then, to create cross-platform filepaths, use os.path.join():
os.path.join(file_dir, "test.txt")
Alternatively, you can change the current working directory to the running file's directory so you don't have to os.path.join every time:
os.path.chdir(os.path.dirname(os.path.abspath(os.path.realpath(__file__))))
Why use os.path.abspath and os.path.realpath? The inner realpath resolves symbolic links, while the abspath resolves relative paths. If you know for sure no symbolic links are being used, you can omit this inner realpath call.
A module's location is always available in the __file__ variable. You can use the functions in os.path (I'm mainly thinking of basedir and join) to transform module-relative paths to absolute paths

python FileNotFoundError

I was writing a program that accesses a .txt file at the start of the program with the open() function. It ran without any errors on the IDE and I was also able to read the text file when running from the IDE without any issues. Although when I ran from the Python Launcher it threw a "FileNotFoundError"
Here's my code:
directions_object = open('warcards_directions.txt','r')
Further to Dan D's comment.. try putting this on the line in front of your open() call:
from os.path import abspath
print(abspath('warcards_directions.txt'))
You'll see that python looks in different places depending on where you run it from .. because it looks for files relative to the current working directory, which changes depending on how you run python.
This is a common problem for new comers. See here How to import files in python using sys.path.append? for some solutions (note the underlying problem in that post is the same as this one.. the fact that they're trying to import a file, and here we're trying to open one is not too important).
Also I'll add that I often reference things relative to the script itself... like this:
from os.path import abspath, join, dirname
script_dir = dirname(__file__)
txt_path = abspath(join(script_dir, "..", "path", "to", "warcards_directions.txt"))
This works if your txt file and your python script stay in the same place relative to each other (but might be installed in different places).
E.g. above assumes your script lives in C:\Foo\scripts\script.py and your text file lives in C:\Foo\path\to\warcards_directions.txt. The method above will work fine where ever you run the script from and it'll work if you move or rename the C:\Foo dir (e.g. to C:\Program Files\Bar). But it'll break if you decide to move scripts.py down a directory into C:\Foo (at which point you change the way txt_path is initialized to fix).
When you said "python launcher", do you mean the command line?
python myScript.py
If you, you will need to cd into the directory where the file is at before you can execute the script. Otherwise, provide the full path to the txt file in your script.

Categories