Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Files in unix and windows

Reply
Thread Tools

Files in unix and windows

 
 
Travis James Kleeburg
Guest
Posts: n/a
 
      08-20-2004
I am trying to copy files from one directory into another using
shutil.copy. I am having a problem getting a usable path using
os.path.abspath because it doesnt return a string with the extra
backslashes and i was wondering if there was a better system call to use?
I am also looking for a system call so there wont be a problem between windows and
unix.


 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      08-21-2004
Which extra (back)slashes do you mean? Do you mean that os.path.abspath
strips the trailing separator characters from its argument?

>>> os.path.abspath("../../../../etc/")

'/etc'
>>> os.path.normpath("./")

'.'

This behavior doesn't seem to be specifically documented.

If preserving the trailing '/' when present is important, you can easily
get this behavior using os.path.abspath:
def myabspath(p):
hadsep = p and p[-1] in (os.sep, os.altsep)
p = os.path.abspath(p)
if hadsep: p = p + os.sep
return p

>>> myabspath('../../../../../etc/')

'/etc/'
>>> myabspath('../../../../../etc')

'/etc'

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBJ0ibJd01MZaTXX0RAg4mAJ9SUjjg8rDt4OY1YvVaNw 8lSx9vAQCbBbk1
E6QV8mAC602+zN7/r8j3+/s=
=qWGo
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Byron
Guest
Posts: n/a
 
      08-21-2004
Hi Travis,

You want to use forward slashes with your file names. Backslashes are
used for special character commands, such as "/t" for <tab>, etc.

For Windows:
c:/aFolder/testDocument.txt

For Unix / Linux
/aFolder/testDocument.txt

If you need a relative path, with no drive specified (for Windows), use:
/aFolder/testDocument.txt

Hope this helps,

Byron
---


Travis James Kleeburg wrote:

> I am trying to copy files from one directory into another using
> shutil.copy. I am having a problem getting a usable path using
> os.path.abspath because it doesnt return a string with the extra
> backslashes and i was wondering if there was a better system call to use?
> I am also looking for a system call so there wont be a problem between windows and
> unix.
>
>

 
Reply With Quote
 
Byron
Guest
Posts: n/a
 
      08-21-2004
Opps, correction. "\t" is for <tab> -- I had the slash going the wrong
direction.

Byron
---


Byron wrote:

> Hi Travis,
>
> You want to use forward slashes with your file names. Backslashes are
> used for special character commands, such as "/t" for <tab>, etc.
>
> For Windows:
> c:/aFolder/testDocument.txt
>
> For Unix / Linux
> /aFolder/testDocument.txt
>
> If you need a relative path, with no drive specified (for Windows),
> use:
> /aFolder/testDocument.txt
>
> Hope this helps,
>
> Byron
> ---
>
>
> Travis James Kleeburg wrote:
>
>> I am trying to copy files from one directory into another using
>> shutil.copy. I am having a problem getting a usable path using
>> os.path.abspath because it doesnt return a string with the extra
>> backslashes and i was wondering if there was a better system call to
>> use? I am also looking for a system call so there wont be a problem
>> between windows and unix.
>>
>>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removingCR/LF from unix and windows and mixed files nntpman68 Perl Misc 3 09-11-2008 11:56 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM



Advertisments