So, I'm trying to incorporate os.path.isfile or os.path.exists into my code with success in finding certain regular files(pdf,png) when searching for filenames that begin with a letter.
The file naming standard that I'm using (and can't change due to the user) starts with a number and subsequently can't be found using the same method. Is there a way that I can make these files discoverable by .isfile or .exists?
The files I'm searching for are .txt files.
os.path.isfile("D:\Users\spx9gs\Project Work\Data\21022013AA.txt")
os.path.isfile("D:\Users\spx9gs\Project Work\Data\AA21022013.txt")
Returns:
False
True
You need to use raw strings, or escape your backslashes. In the filename:
"D:\Users\spx9gs\Project Work\Data\21022013AA.txt"
the \210 will be interpreted as an octal escape code so you won't get the correct filename.
Either of these will work:
r"D:\Users\spx9gs\Project Work\Data\21022013AA.txt"
"D:\\Users\\spx9gs\\Project Work\\Data\\21022013AA.txt"
Related
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')
I'm writing a script that will take a list of file paths as input. I want the script to make sure the strings in the input file are, or at least appear to be, valid full Windows paths, that include the drive letter.
That being said, what's the best way to ensure that a string starts with any single letter, upper or lowercase, a colon, and a back slash?
I'm guessing the regex would look something like this:
[a-zA-Z]:\, but how do I make sure it check for only one letter and that it's the first 3 characters in the string?
I appreciate it.
The ^ character matches the start of the string. Your character class will currently only match one letter, and you need to escape the \. So your final regex would be:
^[a-zA-Z]:\\
If you just want to check to make sure it starts with a drive letter, you could also use the built-in splitdrive:
drive, path = os.path.splitdrive(filename)
if drive == None:
raise ValueError, 'Filename does not include a drive!'
Edit: Thanks to jme, if you are not on a Windows system, do import ntpath and replace the first line like this:
drive, path = ntpath.splitdrive(filename)
Note: since Python 2.7.8, splitdrive returns a 'drive' for UNC paths too.
Checks that the given path is a valid windows path based on criteria at http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx. I made this a little while ago out of frustration of not finding a good one online:
r'^(?:[a-zA-Z]:\\|\\\\?|\\\\\?\\|\\\\\.\\)?(?:(?!(CLOCK\$(\\|$)|(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9]| )(?:\..*|(\\|$))|.*\.$))(?:(?:(?![><:/"\\\|\?\*])[\x20-\u10FFFF])+\\?))*$'
So this is what ended up working for me.
is_path = re.match("^[a-zA-Z]:\\)*", file)
I have a string which contains user input for a directory address on a linux system. I need to check if it is properly formatted and could be an address in Python 2.6. It's important to note that this is not on the current system so I can't check if it is there using os.path nor can I try to create the directories as the function will be run many times.
These strings will always be absolute paths, so my first thought was to look for a leading slash. From there I wondered about checking if the rest of the string only contains valid characters and does not contain any double slashes. This seems a little clunky, any other ideas?
Sure the question has been edited since writing this but:
There is the os.path.isabs(PATH) which will tell you if the path is absolute or not.
Return True if path is an absolute pathname. On Unix, that means it begins with a slash, on Windows that it begins with a (back)slash after chopping off a potential drive letter.
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.
It is inspired by "How to make a valid Windows filename from an arbitrary string?", I've written a function that will take arbitrary string and make it a valid filename.
My function should technically be an answer to this question, but I want to make sure I've not done anything stupid, or overlooked anything, before posting it as an answer.
I wrote this as part of tvnamer - a utility which takes TV episode filenames, and renames them nice and consistently, with an episode pulled from http://www.thetvdb.com - while the source filename must be a valid file, the series name is corrected, and the episode name - so both could contain theoretically any characters. I'm not so much concerned about security as usability - it's mainly to prevent files being renamed .some.series - [01x01].avi and the file "disappearing" (rather than to thwart evil people)
It makes a few assumptions:
The filesystem supports Unicode filenames. HFS+ and NTFS both do, which will cover a majority of users. There is also a normalize_unicode argument to strip out Unicode characters (in tvnamer, this is set via the config XML file)
The platform is either Darwin, Linux, and everything else is treated as Windows
The filename is intended to be visible (not a dotfile like .bashrc) - it would be simple enough to modify the code to allow .abc format filenames, if desired
Things I've (hopefully) handled:
Prepend underscore if filename starts with . (prevents filenames . .. and files from disappearing)
Remove directory separators: / on Linux, and / and : on OS X
Removing invalid Windows filename characters \/:*?"<>| (when on Windows, or forced with windows_safe=True)
Prepend reserved filenames with underscore (COM2 becomes _COM2, NUL becomes _NUL etc)
Optional normalisation of Unicode data, so å becomes a and non-convertable characters are removed
Truncation of filenames over 255 characters on Linux/Darwin, and 32 characters on Windows
The code and a bunch of test-cases can be found and fiddled with at http://gist.github.com/256270. The "production" code can be found in tvnamer/utils.py
Is there any errors with this function? Any conditions I've missed?
One point I've noticed: Under NTFS, some files can not be created in specific directories.
E.G. $Boot in root