473,508 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The format of filename

Where can I find documentation of what Python accepts as the
filename argument to the builtin function file?

As an example, I'm aware (through osmosis?) that I can use '/' as
a directory separator in filenames on both Unix and Dos. But
where is this documented?

--
Neil Cerutti
Oct 24 '06 #1
19 2331
Neil Cerutti wrote:
Where can I find documentation of what Python accepts as the
filename argument to the builtin function file?
Python will accept whatever the OS accepts.
As an example, I'm aware (through osmosis?) that I can use '/' as
a directory separator in filenames on both Unix and Dos. But
where is this documented?
It's documented in the OS's documentation. It can be queried with os.sep
and os.altsep.
Oct 24 '06 #2
Neil Cerutti wrote:
Where can I find documentation of what Python accepts as the
filename argument to the builtin function file?

As an example, I'm aware (through osmosis?) that I can use '/' as
a directory separator in filenames on both Unix and Dos. But
where is this documented?
in the documentation for your operating system. Python doesn't do
anything with the filenames.

</F>

Oct 24 '06 #3
On 2006-10-24, Leif K-Brooks <eu*****@ecritters.bizwrote:
Neil Cerutti wrote:
>As an example, I'm aware (through osmosis?) that I can use '/'
as a directory separator in filenames on both Unix and Dos.
But where is this documented?

It's documented in the OS's documentation. It can be queried
with os.sep and os.altsep.
Thanks. The contents of 6.1.6 Miscellanious System Information
seems to be what I'm looking for.

--
Neil Cerutti
I've had a wonderful evening, but this wasn't it. --Groucho Marx
Oct 24 '06 #4
On 2006-10-24, Fredrik Lundh <fr*****@pythonware.comwrote:
Neil Cerutti wrote:
>Where can I find documentation of what Python accepts as the
filename argument to the builtin function file?

As an example, I'm aware (through osmosis?) that I can use '/' as
a directory separator in filenames on both Unix and Dos. But
where is this documented?

in the documentation for your operating system. Python doesn't
do anything with the filenames.
Is translation of '/' to '\\' a feature of Windows or Python?

--
Neil Cerutti
Oct 24 '06 #5
Neil Cerutti wrote:
Is translation of '/' to '\\' a feature of Windows or Python?
It's a feature of Windows, but it isn't a translation. Both slashes are
valid path separators on Windows; backslashes are just the canonical form.
Oct 24 '06 #6
On 2006-10-24, Neil Cerutti <ho*****@yahoo.comwrote:
Is translation of '/' to '\\' a feature of Windows or Python?
Well, *that* was easy to discover on my own. ;-)

Thanks for the pointers.

--
Neil Cerutti
Oct 24 '06 #7
Neil Cerutti wrote:
Is translation of '/' to '\\' a feature of Windows or Python?
Windows. Random MSDN link:

http://msdn2.microsoft.com/en-us/library/77859s1t.aspx

Win32 operating systems support both the backslash (\) and
the forward slash (/). /.../ (However, the Windows operating
system command shell, CMD.EXE, does not support the forward
slash in commands entered at the command prompt.)

for general file naming guidelines for Windows (including the shell), see:

http://msdn.microsoft.com/library/en...ing_a_file.asp

</F>

Oct 24 '06 #8
>As an example, I'm aware (through osmosis?) that I can use '/' as
>a directory separator in filenames on both Unix and Dos. But
where is this documented?

in the documentation for your operating system. Python doesn't do
anything with the filenames.
Windows seems to be (occasionally) doing the translation as /F
mentions:

C:\temppython
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more
information.
>>for line in file('subdir/test.txt'):
.... print line.strip()
....
1
2
3
>>^Z
C:\tempREM try the same filename convention from dos prompt
C:\temptype subdir/test.txt
The syntax of the command is incorrect.
C:\tempREM try with quotes, just in case...
C:\temptype "subdir/test.txt"
The syntax of the command is incorrect.
C:\tempnotepad subdir/test.txt
C:\tempREM correctly opened the text file in notepad

Windows seems to doing the translation inconsistently (I know
that comes as a shock...)

-tkc

Oct 24 '06 #9
On 2006-10-24, Neil Cerutti <ho*****@yahoo.comwrote:
On 2006-10-24, Neil Cerutti <ho*****@yahoo.comwrote:
>Is translation of '/' to '\\' a feature of Windows or Python?

Well, *that* was easy to discover on my own. ;-)

Thanks for the pointers.
Some experimentation shows that Python does seem to provide
*some* translation. Windows lets me use '/' as a path separator,
but not as the name of the root of a partition name. But perhaps
this a peculiarity of the commands themselves, and not of Windows
path names in particular.

C:\PYTHON24>CD /
The syntax of the command is incorrect.

C:\PYTHON24>CD \
C:\>EDIT /PYTHON24/README
The syntax of the command is incorrect.

--
Neil Cerutti
The outreach committee has enlisted 25 visitors to make calls on
people who are not afflicted with any church. --Church Bulletin
Blooper
Oct 24 '06 #10
Tim Chase wrote:
Windows seems to be (occasionally) doing the translation as /F
mentions:
as leif pointed out, there's no translation.
C:\tempREM try the same filename convention from dos prompt
C:\temptype subdir/test.txt
The syntax of the command is incorrect.
C:\tempREM try with quotes, just in case...
C:\temptype "subdir/test.txt"
The syntax of the command is incorrect.
"type" is built into the command shell. it's the command shell that
refuses to deal with forward slashes, not the underlying API:s.
Windows seems to doing the translation inconsistently (I know
that comes as a shock...)
it's a documented inconsistency, which is preserved mostly for
historical reasons (the shell syntax hasn't changed much since
the CP/M days)

</F>

Oct 24 '06 #11
Tim Chase wrote:
>>>As an example, I'm aware (through osmosis?) that I can use '/' as
a directory separator in filenames on both Unix and Dos. But
where is this documented?

in the documentation for your operating system. Python doesn't do
anything with the filenames.


Windows seems to be (occasionally) doing the translation as /F
mentions:

C:\temppython
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more
information.
>>for line in file('subdir/test.txt'):
.... print line.strip()
....
1
2
3
>>^Z

C:\tempREM try the same filename convention from dos prompt
C:\temptype subdir/test.txt
The syntax of the command is incorrect.
C:\tempREM try with quotes, just in case...
C:\temptype "subdir/test.txt"
The syntax of the command is incorrect.
C:\tempnotepad subdir/test.txt
C:\tempREM correctly opened the text file in notepad

Windows seems to doing the translation inconsistently (I know
that comes as a shock...)
The command line processor parses the slash as a switch delimiter, but
the system call interface doesn't know about such refinements and treats
forward and backward slashes as filename component separators.

Just the same it's always cleanest to use os.path routines (or similar)
to analyze and construct filenames.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 24 '06 #12

Tim Chase wrote:
>
C:\tempREM try the same filename convention from dos prompt
C:\temptype subdir/test.txt
The syntax of the command is incorrect.
C:\tempREM try with quotes, just in case...
C:\temptype "subdir/test.txt"
The syntax of the command is incorrect.
C:\tempnotepad subdir/test.txt
C:\tempREM correctly opened the text file in notepad

Windows seems to doing the translation inconsistently (I know
that comes as a shock...)
Translation? win3.1 to win95 I guess.

Oct 24 '06 #13
Neil Cerutti <ho*****@yahoo.comwrote:
>
Some experimentation shows that Python does seem to provide
*some* translation. Windows lets me use '/' as a path separator,
but not as the name of the root of a partition name. But perhaps
this a peculiarity of the commands themselves, and not of Windows
path names in particular.

C:\PYTHON24>CD /
The syntax of the command is incorrect.

C:\PYTHON24>CD \
C:\>EDIT /PYTHON24/README
The syntax of the command is incorrect.
The Windows APIs all accept either forward slashes or back, and have done
so clear back to Windows 3.0. However, the Windows command shells do not.
That's what you're seeing here.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 26 '06 #14
On 2006-10-26, Tim Roberts <ti**@probo.comwrote:
Neil Cerutti <ho*****@yahoo.comwrote:
>>
Some experimentation shows that Python does seem to provide
*some* translation. Windows lets me use '/' as a path separator,
but not as the name of the root of a partition name. But perhaps
this a peculiarity of the commands themselves, and not of Windows
path names in particular.

C:\PYTHON24>CD /
The syntax of the command is incorrect.

C:\PYTHON24>CD \
C:\>EDIT /PYTHON24/README
The syntax of the command is incorrect.

The Windows APIs all accept either forward slashes or back, and
have done so clear back to Windows 3.0. However, the Windows
command shells do not. That's what you're seeing here.
Well, kudos to Windows then for making Python look really smart
while making itself look like a dufus. ;-)

Seriously, experiments show the forward slash is OK as a
seperator, it just can't be root.

--
Neil Cerutti
Oct 26 '06 #15
Neil Cerutti wrote:
Seriously, experiments show the forward slash is OK as a
seperator, it just can't be root.
do you think you can figure out why, even without reading the various MSDN
pages you've been pointed to ?

</F>

Oct 26 '06 #16
On 2006-10-26, Fredrik Lundh <fr*****@pythonware.comwrote:
Neil Cerutti wrote:
>Seriously, experiments show the forward slash is OK as a
seperator, it just can't be root.

do you think you can figure out why, even without reading the
various MSDN pages you've been pointed to ?
Too late. Plus somebody else already pointed it out: command line
arguments.

--
Neil Cerutti
Oct 26 '06 #17
>Some experimentation shows that Python does seem to provide
>*some* translation. Windows lets me use '/' as a path separator,
but not as the name of the root of a partition name. But perhaps
this a peculiarity of the commands themselves, and not of Windows
path names in particular.

C:\PYTHON24>CD /
The syntax of the command is incorrect.

C:\PYTHON24>CD \
C:\>EDIT /PYTHON24/README
The syntax of the command is incorrect.

The Windows APIs all accept either forward slashes or back, and have done
so clear back to Windows 3.0. However, the Windows command shells do not.
That's what you're seeing here.
What one finds is that the command-shell is self-inconsistant:

C:\temp>md foo
C:\temp>md foo\bar
C:\temp>cd foo/bar
C:\temp\foo\bar>cd ..
C:\temp\foo>cd ..
C:\temp>cd /foo/bar
C:\temp\foo\bar>cd \temp
C:\temp>md foo/baz
The syntax of the command is incorrect
C:\temp>echo dir foo/bar/pip
C:\temp>echo dir /foo/bar/pip
The system cannot find the path specified.
C:\temp>dir foo/bar
Parameter format not correct - "bar".
C:\temp>dir /b foo\bar
pip
C:\temp>type foo/bar/pip
The syntax of the command is incorrect.
C:\temp>type foo\bar\pip
dir
C:\temp>cmd < foo/bar/pip
[directory listing within a subshell]
C:\temp>cmd < /foo/bar/pip
The system cannot find the path specified.
The CD, MD, TYPE, ECHO and redirection are all shell-builtins,
yet they seem to be conflictingly quasi-smart about forward slashes.

Sometimes leading slashes matter. Sometimes they don't.
Sometimes forward slashes are acceptable. Sometimes they aren't.
All within the shell's built-in mechanisms. Go figure.

-tkc


Oct 26 '06 #18
Tim Roberts wrote:
>C:\temp\foo\bar>cd ..
C:\temp\foo>cd ..
C:\temp>cd /foo/bar

This one does not work for me, but as long as the initial slash is back,
the rest can be foreward:

C:\tmp>cd /foo/bar
The system cannot find the path specified.
C:\tmp>cd \foo/bar
C:\foo\bar>

What OS are you running? Do you have the shell extensions enabled? The
latest cd does take a parameter (/d), so I wouldn't expect it to work.
This is cmd.exe on XP (SP2) with shell extensions enabled.

Note the subtle difference though...your "foo" is in the root
directory...mine is in the current directory. My result of

C:\temp>cd /foo/bar

is

C:\temp\foo\bar>

not

C:\foo\bar>
>C:\temp>cmd < foo/bar/pip
[directory listing within a subshell]

I'm also surprised by this one. I'll file this away for my next NT
trivia contest...
You're a sick, sick fellow. :) I just stumbled across it while
trying various shot-in-the-dark permutations of command calls
with f-slashes and b-slashes in paths.
>C:\temp>cmd < /foo/bar/pip
The system cannot find the path specified.
And strangely,

C:\temp>cmd < /temp/foo/bar/pip

fails as well. In more misadventures:

C:\temp>echo dir zip
C:\temp>move zip foo/bar
C:\temp>dir /b foo\bar
pip
zip
C:\temp>move foo/bar/zip .
The system cannot find the path specified.

Okay...MOVE likes it as a destination, but not as a source.
Aaaggghh...If I had any hair, I'd be pulling it out. Move and
copy are similar options. Let's try copy:

C:\temp>move foo\bar\zip .
C:\temp>copy zip foo/bar
1 file(s) copied.
C:\temp>del zip
C:\temp>copy foo/bar/zip .
foo\zip
100% copied 1 file(s) copied.

<sarcasm>Of course! MOVE only accepts forward slashes in
destinations, not the source. But COPY accepts either.</sarcasm>
Maybe?

C:\temp>md foo\baz
C:\temp>dir /s/b
c:\temp\foo
c:\temp\zip
c:\temp\foo\bar
c:\temp\foo\baz
c:\temp\foo\zip
c:\temp\foo\bar\pip
c:\temp\foo\bar\zip

There's no "zip" in the "baz" directory, so let's copy it there:

C:\temp>copy foo/bar/zip foo/baz
foo\zip
Overwrite foo\zip? (Yes/No/All): y
The file cannot be copied onto itself.
0 file(s) copied.

What the [expletive]!? Well, there's a "zip" in our temp
directory, but no "pip". How about we try copying "pip" to the
"baz" directory instead while keeping the same syntax as above?

C:\temp>copy foo/bar/pip foo/baz
The syntax of the command is incorrect.

BLOODY MADNESS!

Some OS programmer doesn't seem to have graduated from their
CompSci program...

-tkc

Oct 26 '06 #19
On 2006-10-26, Tim Chase <py*********@tim.thechases.comwrote:
There's no "zip" in the "baz" directory, so let's copy it
there:

C:\temp>copy foo/bar/zip foo/baz
foo\zip
Overwrite foo\zip? (Yes/No/All): y
The file cannot be copied onto itself.
0 file(s) copied.

What the [expletive]!? Well, there's a "zip" in our temp
directory, but no "pip". How about we try copying "pip" to the
"baz" directory instead while keeping the same syntax as above?

C:\temp>copy foo/bar/pip foo/baz
The syntax of the command is incorrect.

BLOODY MADNESS!

Some OS programmer doesn't seem to have graduated from their
CompSci program...
It comes from every DOS command traditionally doing it's own path
globbing and unglobbing since the shell doesn't do it. You would
think that a library would've been available to DOS programmers
to unify that procedure, but apparently not.

--
Neil Cerutti

Oct 26 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7409
by: Alexander Schmidt | last post by:
Hi, I am not very familiar with C++ programming, so before I do a dirty hack I ask for a more elegant solution (but only the usage of STL is allowed, no special libs). So I need to read a file...
5
2439
by: Jim McGivney | last post by:
In an aspx page I run the following C# code-behind: string filename = null; filename = txtUpload.PostedFile.FileName.Trim(); //save the file...
2
2932
by: Pat | last post by:
Here is my problem that I am having with a current project. I went and backed up all my hard drives to DVD's. After backing up the drives, I used a batch file to get all the file names from the...
4
2421
by: Dave | last post by:
I am writing a C# app in which I need to enumerate the processes running on the PC. I have succesfully done this as follows (assuming the app is running on NT4, XP, 2000 or 2003): ...
14
2361
by: Manish | last post by:
The project I am developing doesn't involves database. I want to parse the mailbox file (.mbx) and store the summary in the text file for fast retrieval and display of information in the Inbox...
4
3482
by: PengYu.UT | last post by:
Suppose I have a dos format text file. The following python code will print ^M at the end. I'm wondering how to print it in unix format. fh = open(options.filename) for line in fh.readlines()...
0
1602
eboyjr14
by: eboyjr14 | last post by:
I have this xml metacity thing, and it is giving off this error: The file format is invalid. <?xml version="1.0"?> <metacity_theme> <info> <name>Royale</name> <author>Devin...
2
2111
by: Dhananjay | last post by:
Hi all , I have got problem when i am tring to exportGridview Data into Excel format. It is going into text format ,but what i want is if the field is number/currency then it should go into...
4
2521
by: krishp | last post by:
Hi, I need to convert raw data recieved from a device through C#.Net socket to BMP format. For some reason the image conversion is not happening proparly and I can see only blur image . ...
5
3758
by: The Pipe | last post by:
Hello I have a table that I need to export to a .asc file format. I have had several attempts at getting this to work but with no luck. Basically the file would need to have every record...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.