Delete a file: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[Perl]]: Without perl modules)
(Added python)
Line 46: Line 46:
perl -e 'unlink "/input.txt"'
perl -e 'unlink "/input.txt"'
perl -e 'rmdir "/docs"'
perl -e 'rmdir "/docs"'

==[[Python]]==

import os
# current directory
os.remove("output.txt")
os.rmdir("docs")
# root directory
os.remove("/output.txt")
os.rmdir("/docs")


==[[Toka]]==
==[[Toka]]==

Revision as of 19:41, 28 July 2007

Task
Delete a file
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the job is to delete a file called "input.txt" and delete a directory called "docs". This should be done twice: once "here", i.e. in the current working directory and once in the filesystem root.


Java

import java.util.File;
public class FileDeleteTest {
   public static boolean deleteFile(String filename) {
       boolean exists = new File(filename).delete();
       return exists;
   }
   public static void test(String type, String filename) {
       System.out.println("The following " + type + " called " + filename + 
           (deleteFile(filename) ? " was deleted." : " could not be deleted.")
       );
   }
   public static void main(String args[]) {
        test("file", "input.txt");
        test("file", File.seperator + "input.txt");
        test("directory", "docs");
        test("directory", File.seperator + "docs" + File.seperator);
   }
}

Perl

use File::Spec::Functions qw(catfile rootdir);
# here
unlink 'input.txt';
rmdir 'docs';
# root dir
unlink catfile rootdir, 'input.txt';
rmdir catfile rootdir, 'docs';

Without Perl Modules

Current directory

perl -e 'unlink input.txt'
perl -e 'rmdir docs'

Root Directory

perl -e 'unlink "/input.txt"'
perl -e 'rmdir "/docs"'

Python

import os
# current directory
os.remove("output.txt")
os.rmdir("docs")
# root directory
os.remove("/output.txt")
os.rmdir("/docs")

Toka

 needs shell
 " docs" remove
 " input.txt" remove