How to open a file in its default program with python - python

I want to open a file in python 3.5 in its default application, specifically 'screen.txt' in Notepad.
I have searched the internet, and found os.startfile(path) on most of the answers. I tried that with the file's path os.startfile(C:\[directories n stuff]\screen.txt) but it returned an error saying 'unexpected character after line continuation character'. I tried it without the file's path, just the file's name but it still didn't work.
What does this error mean? I have never seen it before.
Please provide a solution for opening a .txt file that works.
EDIT: I am on Windows 7 on a restricted (school) computer.

It's hard to be certain from your question as it stands, but I bet your problem is backslashes.
[EDITED to add:] Or actually maybe it's something simpler. Did you put quotes around your pathname at all? If not, that will certainly not work -- but once you do, you will find that then you need the rest of what I've written below.
In a Windows filesystem, the backslash \ is the standard way to separate directories.
In a Python string literal, the backslash \ is used for putting things into the string that would otherwise be difficult to enter. For instance, if you are writing a single-quoted string and you want a single quote in it, you can do this: 'don\'t'. Or if you want a newline character, you can do this: 'First line.\nSecond line.'
So if you take a Windows pathname and plug it into Python like this:
os.startfile('C:\foo\bar\baz')
then the string actually passed to os.startfile will not contain those backslashes; it will contain a form-feed character (from the \f) and two backspace characters (from the \bs), which is not what you want at all.
You can deal with this in three ways.
You can use forward slashes instead of backslashes. Although Windows prefers backslashes in its user interface, forward slashes work too, and they don't have special meaning in Python string literals.
You can "escape" the backslashes: two backslashes in a row mean an actual backslash. os.startfile('C:\\foo\\bar\\baz')
You can use a "raw string literal". Put an r before the opening single or double quotes. This will make backslashes not get interpreted specially. os.startfile(r'C:\foo\bar\baz')
The last is maybe the nicest, except for one annoying quirk: backslash-quote is still special in a raw string literal so that you can still say 'don\'t', which means you can't end a raw string literal with a backslash.

The recommended way to open a file with the default program is os.startfile. You can do something a bit more manual using os.system or subprocess though:
os.system(r'start ' + path_to_file')
or
subprocess.Popen('{start} {path}'.format(
start='start', path=path_to_file), shell=True)
Of course, this won't work cross-platform, but it might be enough for your use case.

For example I created file "test file.txt" on my drive D: so file path is 'D:/test file.txt'
Now I can open it with associated program with that script:
import os
os.startfile('d:/test file.txt')

Related

Exporting multiple csv files with dynamic naming

I created about 200 csv files in Python and now need to download them all.
I created the files from a single file using:
g = df.groupby("col")
for n,g in df.groupby('col'):
g.to_csv(n+'stars'+'.csv')
When I try to use this same statement to export to my machine I get a syntax error and I'm not sure what I'm doing wrong:
g = df.groupby("col")
for n,g in df.groupby('col'):
g.to_csv('C:\Users\egagne\Downloads\'n+'stars'+'.csv'')
Error:
File "<ipython-input-27-43a5bfe55259>", line 3
g.to_csv('C:\Users\egagne\Downloads\'n+'stars'+'.csv'')
^
SyntaxError: invalid syntax
I'm in Jupyter lab, so I can download each file individually but I really don't want to have to do that.
You're possibly mixing up integers and strings, and the use of backslash in literals is dangerous anyway. Consider using the following
import os
inside the loop
f_name = os.path.join('C:', 'users', ' egagne', 'Downloads', str(n), 'stars.csv')
g.to_csv(f_name)
with os.path.join taking care of the backslashes for you.
g.to_csv('C:\Users\egagne\Downloads\'n+'stars'+'.csv'')
needs to be
g.to_csv('C:\\Users\\egagne\\Downloads\\'+n+'stars.csv').
There were two things wrong -- the backslash is an escape character so if you put a ' after it, it will be treated as part of your string instead of a closing quote as you intended it. Using \\ instead of a single \ escapes the escape character so that you can include a backslash in your string.
Also, you did not pair your quotes correctly. n is a variable name but from the syntax highlighting in your question it is clear that it is part of the string. Similarly you can see that stars and .csv are not highlighted as part of a string, and the closing '' should be a red flag that something has gone wrong.
Edit: I addressed what is causing the problem but Ami Tavory's answer is the right one -- though you know this is going to run on windows it is a better practice to use os.path.join() with directory names instead of writing out a path in a string. str(n) is also the right way to go if you are at all unsure about the type of n.

Saving file with apostrophe in the name (Python 3.4)

Trying to save image files in batches. Works nicely, but the list of names for each file sometimes includes apostrophes, and everything stops.
The offending script is:
pic.save(r"C:\Python34\Scripts\{!s}.jpg".format(name))
The apostrophes in the names aren't a problem when I embed them in a url with selenium
browser.get("https://website.com/{!s}".format(name))
or when I print the destination file name, e.g.
print(r"C:\Python34\Scripts\{!s}.jpg".format(name))
Which is fine to turn out like
C:\Python34\Scripts['It's fine'].jpg
so I assume this kind of problem has something to do with the save function.
The trace back calls the pic.save line of code in PIL\Image.py and says the OSError: [Errno 22] is an Invalid argument in the save destination.
Using Windows 7 if that matters.
Probably super-novice error, but I've been reading threads and can't figure this out--workaround would be cleaning the list of apostrophes before using it, which would be annoying but acceptable.
Any help appreciated.
---edited to fix double quotes as single, just mistyped when writing this post...doh.
It's not a Python problem, but Windows, or rather the file system, file naming rules. From the MSDN:
Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
The following reserved characters
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
On UNIX type systems, all except the / would be valid (although most would be a bad idea). A further "character", binary zero 0x00, is invalid on most file systems.
Rules for URLs are different again.
So you are going to have to write a sanitiser for filenames avoiding these characters. A regular expression would probably be the easiest, but you will have to choose replacement characters that don't occur naturally.
Edit: I was assuming that Error 22 was reporting an invalid filename, but I was wrong, it actually means "The device does not recognise the command".
See https://stackoverflow.com/questions/19870570/pil-giving-oserror-errno-22-when-opening-gif. The accepted reply is rather weird though.
I Google'd "python PIL OSError Errno 22", you might like to try the same and see if any of the conditions apply to you, but clearly you are not alone, if that's any consolation.
Sorry I can't do more.

Escaping double-quotes vs. single-quotes

I noticed this code:
os.system("'{0}'".format(path))
and saw that some one had change it to this:
os.system("\"{0}\"".format(path))
I was wondering by changing it from single to double quotes what advantages does it give you?
Here is the original commit I pulled it from: https://github.com/mattn/legit/commit/84bd1b1796b749a7fb40e0b734d2de29ddc9d3d9
Not much really but rule of thumb use single quotes for literal assignments and prints. That way you will avoid printing things that shouldn't be there in the first place.
Single quotes are often useful because they are literal, and contain exactly the characters you type e.g. 'Hi there/' will actually print Hi there/
However, if you need something like 'Hi there /n', if you put it in single quotes it will give you literally 'Hi there /n' whereas double quotes will give you the result you need "Hi there" and then break line.
On windows, command line arguments are parsed by the program it-self, not shell or cmd.exe. And most of windows programs parse quoted strings with double quote in generally. python.exe is the same. On unix OSs, command line arguments are parsed by shell. And most of shells parse single/double quote both. of course, double quote expand $ or something which the shell can treat. However, path will not contains $.
This change is workaround to be possible to work legit on many OSs.

Why does Python (IronPython) report "Illegal characters in path" when the word bin is used?

I am getting an "Illegal characters in path" error when doing chdir commands in Iron Python. This is happening in run time with my code, but even in the Iron Python console it has this issue. I'm using the nt module because in code the os module does not work (appears to be a known issue).
Doing a little bit of playing around it turns out the "illegal characters" is actually the word bin. Below is the text from the console that shows me getting the error only when i navigate to the bin directory.
Here is the example
>>> nt.chdir('c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx')
>>> nt.chdir('c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx\Directory')
>>> nt.chdir('c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx\Directory\bin')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Illegal characters in path.
Whats worse is I'll navigate to a totally different directory (that doesn't even have a bin directory) and try to navigate to a subdirectory "bin" and i'll still get that error!
Any Ideas?
The \ path separator is also a python escape character. Double them, or better yet, use r'' raw python literals instead:
r'c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx'
'c:\\Users\\xxxxx\\Documents\\Visual Studio 2010\\Projects\\xxx'
For example, \n is a newline character, and \t is interpreted as a TAB. In your specific case, \b is interpreted as a backspace.
\ is an escape character in Python strings. \b is backspace, which is why it barfs on \bin: you are not specifying the directory Directory\bin, you are specifying the directory Directory<backspace>in, which is not a legal path and even if it were, does not exist.
You can write the string by doubling the backslashes or by using the r indicator as suggested by Martijn. A third alternative is to simply use forward slashes as in every other civilized operating system. Windows is perfectly happy to use these.
Windows and most other operating systems will recognize the forward slashes. So, instead of the backslash, double-backslash, or r' ' (more on the string literals here) you can just use forward slashes and you are good to go. The answer here is also well detailed.
UPDATE: PS: Use backslashes and string literals with caution. Always check for your specific case. There is a good comment on that in this answer

Formatting File Paths

I'm new to Python so I may be going about this completely wrong, but I'm having problems getting and changing to the directory of a file. My script takes in multiple file names that can be in any directory. In my script I need python to change to the directory of the file and then perform some actions. However, I'm having problems changing directories.
Here is what I've tried so far:
path=os.path.split(<file path>)
os.chdir(path[0])
<Do things to file specified by path[1]>
The way I've been getting the file path is by dragging from explorer to the command line. This enters the path name as something like "C:\foo\bar\file_name.txt" . When I run the first line in the interpreter I get out ('C:\\foo\bar','file_name.txt'). The problem is that for some reason the last backslash isn't automatically escaped so when I run the os.chdir(path[0]) line I get errors.
My question is why is the last backslash not being automatically escaped like the others? How can I manually escape the last backslash? Is there a better way to get the file's directory and change to it?
The last backslash isn't being automatically escaped because Python only escapes backslashes in regular strings when the following character does not form an escape sequence with the backslash. In fact, in your example, you would NOT get 'C:\\foo\bar' from 'C:\foo\bar', you'd get 'C:\x0coo\x08ar'.
What you want to do is either replace the backslashes with forwardslashes, or to make it simpler for drag-and-drop operations, just prepend the path with r so that it's a raw string and doesn't recognize the escape sequences.
>>> os.path.split(r"C:\foo\bar\file_name.txt")
('C:\\foo\\bar','file_name.txt')
You're using the right modules and methods. Just when you're putting that windows path in there, make the string a raw string, so your command should look like:
path=os.path.split(r'C:\foo\bar\file_name.txt')
Note the r in front of the first quote, that makes Python not treat the backslashes in the string as escape sequences.

Categories