error sending path to Win OS

E

Earl Eiland

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
 
M

Michael Hoffman

Earl said:
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
 
P

Paul Watson

Earl Eiland said:
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.'\r\n'
 
E

Earl Eiland

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(InputDirectory, 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(InputDirectory, 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
 
E

Earl Eiland

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(InputDirectory, 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(InputDirectory, 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
 
D

Dennis Lee Bieber

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(InputDirectory , 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(InputDirectory, 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(InputDirectory, 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.

--
 
T

Tim Roberts

Earl Eiland said:
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(InputDirectory, 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(InputDirectory, 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,757
Latest member
PDIJaclyn

Latest Threads

Top