chdir and directory name with whitespace

Discussion in 'Perl Misc' started by als, Sep 22, 2005.

  1. als

    als Guest

    Hi,
    I have a problem with chdir function when the name of a directory have
    a whitespace. Ex. "/zzz/yyy/new york"
    The chdir,in this case, it doesn't work.
    The path is not constant but I get this with a list of directories from
    one root.

    Thanks.
     
    als, Sep 22, 2005
    #1
    1. Advertising

  2. als

    Paul Lalli Guest

    als wrote:
    > I have a problem with chdir function when the name of a directory have
    > a whitespace. Ex. "/zzz/yyy/new york"
    > The chdir,in this case, it doesn't work.


    "does not work" is the worst possible of all error descriptions. What
    actually happens? What's the error message?

    Have you read the Posting Guidelines for this group?

    $ mkdir foo
    $ mkdir foo/bar
    $ mkdir "foo/bar/new york"
    $ perl -e'chdir "foo/bar/new york" or die $!; print `pwd`; '
    /home/mritty/foo/bar/new york
    $

    Seems to work pretty well for me.... Perhaps you should follow the
    Posting Guidelines and post a short-but-complete script that
    demonstrates your problem.

    Paul Lalli
     
    Paul Lalli, Sep 22, 2005
    #2
    1. Advertising

  3. als wrote:
    > I have a problem with chdir function when the name of a directory have
    > a whitespace. Ex. "/zzz/yyy/new york"
    > The chdir,in this case, it doesn't work.


    Would you mind showing us a short but complete program that demonstrates
    the problem?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 22, 2005
    #3
  4. als

    Babacio Guest

    "als" <> writes:

    > Hi,
    > I have a problem with chdir function when the name of a directory have
    > a whitespace. Ex. "/zzz/yyy/new york"
    > The chdir,in this case, it doesn't work.


    It should work. Which system do you use ?

    > The path is not constant but I get this with a list of directories
    > from one root.


    Is it a relevant information ? More precisely : does this mean that
    you can't reproduce the bug with a constant path ?

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Sep 22, 2005
    #4
  5. als

    als Guest

    Hi and thanks all,
    more in deep (it's only an example):

    opendir(MYDIR, $rootdir);
    my (@files) = readdir MYDIR;
    foreach $subdir (@files) {
    if ($subdir ne '.' && $subdir ne '..' && (-d "$rootdir/$subdir")) {
    chdir ("$rootdir/$subdir") or die "WHITESPACE";
    }
    }

    yes, it's mean that I can't reproduce the bug with a constant path.
     
    als, Sep 22, 2005
    #5
  6. als wrote:
    >
    > opendir(MYDIR, $rootdir);
    > my (@files) = readdir MYDIR;
    > foreach $subdir (@files) {
    > if ($subdir ne '.' && $subdir ne '..' && (-d "$rootdir/$subdir")) {
    > chdir ("$rootdir/$subdir") or die "WHITESPACE";

    ----------------^^^^^^^^^^^^^^^^

    One thought is that you'd better use File::Spec for a safer way to
    concatenate into paths.

    use File::Spec;
    opendir MYDIR, $rootdir or die "Couldn't open $rootdir: $!";
    my @files = readdir MYDIR;
    foreach my $subdir (@files) {
    my $path = File::Spec->catfile($rootdir, $subdir);
    if ($subdir ne '.' && $subdir ne '..' && -d $path) {
    chdir $path or die "WHITESPACE";
    }
    }
    closedir MYDIR;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 22, 2005
    #6
  7. als

    Paul Lalli Guest

    als wrote:
    > Hi and thanks all,
    > more in deep (it's only an example):


    Stop doing that. Give a *real* piece of code.

    > opendir(MYDIR, $rootdir);


    You haven't checked to see whether or not this opendir succeeds.

    opendir my $MYDIR, $rootdir or die "Cannot open $rootdir: $!";

    > my (@files) = readdir MYDIR;


    (Now that we're using a lexical directory handle, this will need to be
    changed too)
    my @files = readdir $MYDIR;

    > foreach $subdir (@files) {
    > if ($subdir ne '.' && $subdir ne '..' && (-d "$rootdir/$subdir")) {
    > chdir ("$rootdir/$subdir") or die "WHITESPACE";


    Why aren't you asking Perl *why* the changedir failed? Include $! in
    your error output.
    chdir "$rootdir/$subdir" or die "Cannot open '$rootdir/$subdir': $!";

    > }
    > }
    >
    > yes, it's mean that I can't reproduce the bug with a constant path.


    "You're doing something wrong." But you still haven't given us enough
    information to actually see what that might be, and you haven't asked
    Perl for all the help it will give you.

    You *are* using strict & warnings too, right?

    Paul Lalli
     
    Paul Lalli, Sep 22, 2005
    #7
  8. als

    Babacio Guest

    "als"

    > yes, it's mean that I can't reproduce the bug with a constant path.


    So the bug is somewhere in the generation of the content of variable.
    Can you reproduce the bug with something like

    $path="/fou/barre/nouvelle orleans/";
    chdir($path);

    I'll bet you can't. If you can, tell us.

    Your piece of code is not what we need. Please try to produce a piece of
    code, with minimal size, which produces the problem.

    Well, before doing that, you may try to replaxe your 'die
    "WHITESPACE"' by a something like
    die "Problem with '$rootdir/$subdir' : $!";

    which may help you to discover yourself where is the problem.

    --
    Bé erre hue ixe eu elle, Bruxelles.
     
    Babacio, Sep 22, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oli Filth
    Replies:
    9
    Views:
    3,333
    Uncle Pirate
    Jan 17, 2005
  2. Jon Schull

    cgi-tb after os.chdir

    Jon Schull, Jul 14, 2003, in forum: Python
    Replies:
    0
    Views:
    410
    Jon Schull
    Jul 14, 2003
  3. Yun Mao

    os.environ and os.path.chdir

    Yun Mao, Sep 20, 2003, in forum: Python
    Replies:
    4
    Views:
    440
    Thomas Bellman
    Sep 20, 2003
  4. Replies:
    10
    Views:
    746
    Eric Brunel
    Dec 16, 2008
  5. MRAB
    Replies:
    3
    Views:
    388
Loading...

Share This Page