By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,995 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,995 IT Pros & Developers. It's quick & easy.

error sending path to Win OS

P: n/a
os.path.getsize(Inputdirectory + '\\' + Filename) works, but
os.path.getsize(Inputdirectory + '\\' + Filename.split('.') + '.ext')
Fails reporting "no such file or directory
InputDirectory\\Filename.ext".
os.path.getsize(Inputdirectory + r'\' + Filename.split('.') + '.ext')
generates a syntax error.

Earl Eiland

Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Earl Eiland wrote:
os.path.getsize(Inputdirectory + '\\' + Filename) works, but
os.path.getsize(Inputdirectory + '\\' + Filename.split('.') + '.ext')
Fails reporting "no such file or directory
InputDirectory\\Filename.ext".
No, that should be a TypeError. This will be easier if you copy and
paste your Python session instead of making stuff up.
os.path.getsize(Inputdirectory + r'\' + Filename.split('.') + '.ext')
generates a syntax error.


'r"\" is not a valid string literal (even a raw string cannot end in an
odd number of backslashes). Specifically, a raw string cannot end in a
single backslash (since the backslash would escape the following quote
character). Note also that a single backslash followed by a newline is
interpreted as those two characters as part of the string, not as a
line continuation.'

http://docs.python.org/ref/strings.html
--
Michael Hoffman
Jul 18 '05 #2

P: n/a
To generate path names take a look at os.path.join(see
http://docs.python.org/lib/module-os.path.html)

Jul 18 '05 #3

P: n/a
"Earl Eiland" <ee*@nmt.edu> wrote in message
news:ma*************************************@pytho n.org...
os.path.getsize(Inputdirectory + '\\' + Filename) works, but
os.path.getsize(Inputdirectory + '\\' + Filename.split('.') + '.ext')
Fails reporting "no such file or directory
InputDirectory\\Filename.ext".
os.path.getsize(Inputdirectory + r'\' + Filename.split('.') + '.ext')
generates a syntax error.

Earl Eiland


Use the language provided os.sep rather than trying to code your own which
will be incorrect when the code is run on a different platform.

$ python
Python 2.1 (#15, May 4 2004, 21:22:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
import os
dir(os) .... os.sep '\\' os.pathsep ';' os.linesep

'\r\n'
Jul 18 '05 #4

P: n/a
A couple of you commented that I should be using os.path.join.
Accordingly, I rewrote my code. Unfortunately, I still have the same
problem. the following code snippet

Results.SetOriginal(os.path.getsize(os.path.join(I nputDirectory , x)))
y = str(x.split('.')[0]) + '.rk'
print InputDirectory, y
raw_input()
Results.SetArchive(os.path.getsize(os.path.join(In putDirectory, y)))

is executed from the command line with
C:\Documents and Settings\eeiland\Desktop> ..\Thesis\Plan2\Compressor.py
Test_Data\ Test_Output\Results
Test_Data\ Book1.rk, where InputDirectory (in the above snippet) =
'Test_Data\' (I've also tried 'Test_Data', with the same results).

x (in the above snippet) is an element of the list generated by
os.listdir(InputDirectory).

Output upon execution is as follows:
Test_Data\ Book1.rk

Traceback (most recent call last):
File "C:\Documents and Settings\eeiland\Thesis\Plan2\Compressor.py",
line 60,
in ?
Results.SetArchive(os.path.getsize(os.path.join(In putDirectory, y)))
File "C:\Python24\lib\ntpath.py", line 229, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 'Test_Data\\Book1.rk'

What am I doing wrong?

Earl
On Sat, 2005-03-12 at 15:16, Michael Hoffman wrote:
Earl Eiland wrote:
os.path.getsize(Inputdirectory + '\\' + Filename) works, but
os.path.getsize(Inputdirectory + '\\' + Filename.split('.') + '.ext')
Fails reporting "no such file or directory
InputDirectory\\Filename.ext".


No, that should be a TypeError. This will be easier if you copy and
paste your Python session instead of making stuff up.
os.path.getsize(Inputdirectory + r'\' + Filename.split('.') + '.ext')
generates a syntax error.


'r"\" is not a valid string literal (even a raw string cannot end in an
odd number of backslashes). Specifically, a raw string cannot end in a
single backslash (since the backslash would escape the following quote
character). Note also that a single backslash followed by a newline is
interpreted as those two characters as part of the string, not as a
line continuation.'

http://docs.python.org/ref/strings.html
--
Michael Hoffman


Jul 18 '05 #5

P: n/a
A couple of you commented that I should be using os.path.join.
Accordingly, I rewrote my code. Unfortunately, I still have the same
problem. the following code snippet

Results.SetOriginal(os.path.getsize(os.path.join(I nputDirectory , x)))
y = str(x.split('.')[0]) + '.rk'
print InputDirectory, y
raw_input()
Results.SetArchive(os.path.getsize(os.path.join(In putDirectory, y)))

is executed from the command line with
C:\Documents and Settings\eeiland\Desktop> ..\Thesis\Plan2\Compressor.py
Test_Data\ Test_Output\Results
Test_Data\ Book1.rk, where InputDirectory (in the above snippet) =
'Test_Data\' (I've also tried 'Test_Data', with the same results).

x (in the above snippet) is an element of the list generated by
os.listdir(InputDirectory).

Output upon execution is as follows:
Test_Data\ Book1.rk

Traceback (most recent call last):
File "C:\Documents and Settings\eeiland\Thesis\Plan2\Compressor.py",
line 60,
in ?
Results.SetArchive(os.path.getsize(os.path.join(In putDirectory, y)))
File "C:\Python24\lib\ntpath.py", line 229, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 'Test_Data\\Book1.rk'

What am I doing wrong?

Earl
On Sat, 2005-03-12 at 15:16, Michael Hoffman wrote:
Earl Eiland wrote:
os.path.getsize(Inputdirectory + '\\' + Filename) works, but
os.path.getsize(Inputdirectory + '\\' + Filename.split('.') + '.ext')
Fails reporting "no such file or directory
InputDirectory\\Filename.ext".


No, that should be a TypeError. This will be easier if you copy and
paste your Python session instead of making stuff up.
os.path.getsize(Inputdirectory + r'\' + Filename.split('.') + '.ext')
generates a syntax error.


'r"\" is not a valid string literal (even a raw string cannot end in an
odd number of backslashes). Specifically, a raw string cannot end in a
single backslash (since the backslash would escape the following quote
character). Note also that a single backslash followed by a newline is
interpreted as those two characters as part of the string, not as a
line continuation.'

http://docs.python.org/ref/strings.html
--
Michael Hoffman


Jul 18 '05 #6

P: n/a
On Mon, 14 Mar 2005 06:54:02 -0700, Earl Eiland <ee*@nmt.edu> declaimed
the following in comp.lang.python:
A couple of you commented that I should be using os.path.join.
Accordingly, I rewrote my code. Unfortunately, I still have the same
problem. the following code snippet
print os.getcwd() #verify what the current directory IS.
fid = os.path.join(InputDirectory, x)
print fid, os.path.exists(fid) Results.SetOriginal(os.path.getsize(os.path.join(I nputDirectory , x))) # replace above
Results.SetOriginal(os.path.getsize(fid)) y = str(x.split('.')[0]) + '.rk' # replace above
y = os.path.splitext(fid)[0] + ".rk"
fid2 = os.path.join(InputDirectory, y)
print fid2, os.path.exists(fid2) print InputDirectory, y # delete above raw_input() # what is this doing? Results.SetArchive(os.path.getsize(os.path.join(In putDirectory, y))) # replace above
Results.SetArchive(os.path.getsize(fid2))
Output upon execution is as follows:
Test_Data\ Book1.rk

Traceback (most recent call last):
File "C:\Documents and Settings\eeiland\Thesis\Plan2\Compressor.py",
line 60,
in ?
Results.SetArchive(os.path.getsize(os.path.join(In putDirectory, y)))
File "C:\Python24\lib\ntpath.py", line 229, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 'Test_Data\\Book1.rk'

What am I doing wrong?
Does "Test_Data\Book1.rk" EXIST (ignore the double \ in the
message, that's just Python's escaping of the slash, as that is what
would be needed for you to type it in a Python statement:
x = "Test_Data\\Book1.rk"
The os.path.exists(), along with the os.getcwd() call, should show where
it is looking, and if the file is there.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7

P: n/a
Earl Eiland <ee*@nmt.edu> wrote:

A couple of you commented that I should be using os.path.join.
Accordingly, I rewrote my code. Unfortunately, I still have the same
problem. the following code snippet

Results.SetOriginal(os.path.getsize(os.path.join( InputDirectory , x)))
y = str(x.split('.')[0]) + '.rk'
print InputDirectory, y
raw_input()
Results.SetArchive(os.path.getsize(os.path.join(I nputDirectory, y)))

is executed from the command line with
C:\Documents and Settings\eeiland\Desktop> ..\Thesis\Plan2\Compressor.py
Test_Data\ Test_Output\Results
Test_Data\ Book1.rk, where InputDirectory (in the above snippet) =
'Test_Data\' (I've also tried 'Test_Data', with the same results).

x (in the above snippet) is an element of the list generated by
os.listdir(InputDirectory).

Output upon execution is as follows:
Test_Data\ Book1.rk

Traceback (most recent call last):
File "C:\Documents and Settings\eeiland\Thesis\Plan2\Compressor.py",
line 60,
in ?
Results.SetArchive(os.path.getsize(os.path.join(In putDirectory, y)))
File "C:\Python24\lib\ntpath.py", line 229, in getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory: 'Test_Data\\Book1.rk'

What am I doing wrong?


My guess would be that Test_Data\Book1.rk does not exist from the point of
view of the directory where the script is running this. You might do this:
print os.getcwd()
os.system('dir ' + InputDirectory)
just to prove that you are where you think you are.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.