简体   繁体   中英

Return value of os.path in Python

For this code:

import os
a=os.path.join('dsa','wqqqq','ffff')
print a
print os.path.exists('dsa\wqqqq\ffff') #what situation this will be print True?

When will os.path.exists('what') print True ?

'dsa\\wqqqq\\ffff' does not mean what you probably think it does: \\f , within a string, is an escape sequence and expands to the same character as chr(12) (ASCII "form feed").

So print os.path.exists('dsa\\wqqqq\\ffff') will print True if:

  • on Windows, there's a subdirectory dsa in the current working directory, and within it a file or subdirectory whose name is equal to the string `'wqqqq' + chr(12) + 'fff'
  • on Linux, Mac, etc, if there's a file or subdirectory in the current directory whose name is equal to the string `'dsa' + '\\' + 'wqqqq' + chr(12) + 'fff'

They seem like two peculiar conditions to check, and that you actually want to check their combination depending on the platform seems even less likely.

More likely you might want to

print os.path.exists(os.path.join('dsa', 'wqqqq', 'ffff'))

which works cross-platform, printing True if in the current working directory there's a subdirectory dsa containing a subdirectory wqqqq containing a file or subdirectory ffff . This avoids messing with backslashes.

If you're keen to have your code perform this check only on Windows (and have very different semantics on all other platforms), you can use

print os.path.exists(r'dsa\wqqqq\ffff')

the leading r in the string literal tells the Python compiler to avoid interpreting backslashes in it (however, don't try to end such a literal with backslash, since the backslash is still taken to escape the quote). Or:

print os.path.exists('dsa\\wqqqq\\ffff')

doubling-up the backslashes works. Note, also, that:

print os.path.exists('dsa/wqqqq/ffff')

with normal slashes instead of backslashes, works just fine in BOTH Windows and elsewhere (which makes it particularly absurd to want to use backslashes here, unless one is deliberately trying to obtain a program that behaves weirdly on non-Windows machines).

The much-simpler other question which you ask in the text after your code is easier: os.path.exists('what') , on any platform, prints True if there is a file or subdirectory named what in the current working directory.

Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

http://docs.python.org/library/os.path.html#os.path.exists

It will print True if the path exists. Not sure what the confusion is, here. Not to be rude, but RTFM.

% mkdir -p dsa/wqqqq/ffff
% cat <<EOF | python
> import os
> a=os.path.join('dsa','wqqqq','ffff')
> print a
> print os.path.exists('dsa/wqqqq/ffff')
> EOF
dsa/wqqqq/ffff
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM