-4

I am using time() function to compute epoch time and using multiplying factor with number of days to find difference between them is greater than modification time.

However I am not able to get list fo files and folders under the specified path and how to remove all the directories, sub directories which are not empty.

Avinash_cdns
  • 165
  • 7
  • 2
    And here we are with yet another Python to Perl question. **Please** feed your question into Google before asking here. – MattDMo Nov 04 '14 at 02:18
  • possible duplicate of [How do I read in the contents of a directory in Perl?](http://stackoverflow.com/questions/22566/how-do-i-read-in-the-contents-of-a-directory-in-perl) – MattDMo Nov 04 '14 at 02:22
  • The method listdir() returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory. – Avinash_cdns Nov 04 '14 at 02:25
  • @MattDMo where does it say questions about problems going between perl and python code are not in scope for SO? – GreenAsJade Nov 04 '14 at 02:32
  • I sure hope that's not the code you're using! As for providing an alternative, you missed out by not actually saying what you want. – ikegami Nov 04 '14 at 03:14
  • 1
    In sync with what exactly? – hobbs Nov 04 '14 at 03:31

2 Answers2

3

Based on what I read from the the python docs, the following wouldn't be equivalent, but it would be close:

do {
   opendir(my $dh, $path)
      or die("Can't open directory \"$path\": $!\n");

   my @files = grep !/^\.\.?\z/, readdir($dh);

   closedir($dh)
      or die("Error reading directory \"$path\": $!\n");

   @files
}

Some differences:

  • It's not really possible to check if readdir fails.
  • The error message thrown is different.
  • The thrown exception wouldn't be of the non-existent OSError class.
  • The exception enclosed in a Python OSError class may not be available to whoever catches the Perl exception.
  • More? I don't know Python.

It would have been better if you had specified what you actually wanted to do rather saying you want the equivalent of some other language's function, since

  • The chances of two languages having exactly the same function is not that good, so you leave us guessing what you actually want.
  • You could actually have searched for that.
ikegami
  • 367,544
  • 15
  • 269
  • 518
1

You're on the right track (though the snippet you pasted is illegal Perl, e.g. it has no semi-colons). Remember that Perl just slurps in the directory, including the special "." and ".." entries, whereas os.listdir doesn't (see the docs).

Of course, Perl being Perl, there is more than one way to do it, e.g. you can use something like glob or File::Find.

AlwaysLearning
  • 796
  • 3
  • 10