How can I create a new file, in which I intend to write in a existing directory using open() in Perl?
I tried like this:
my $existingdir = './mydirectory';
open my $fileHandle, ">>", "$existingdir/filetocreate.txt" or die "Can't open '$existingdir/filetocreate.txt'\n";
But it won't work.
open (FH, '>', “filename. txt”); If the file is existing then it truncates the old content of file with the new content. Otherwise a new file will be created and content will be added.
Use Sys::Syslog to write log messages. But since you're opening a log. txt file with the handle OUTPUT , just change your two print statements to have OUTPUT as the first argument and the string as the next (without a comma). Not only because it's in your while loop.
Write array to a file : file write « File « Perl Writes to the standard error file.
my $existingdir = './mydirectory';
mkdir $existingdir unless -d $existingdir; # Check if dir exists. If not create it.
open my $fileHandle, ">>", "$existingdir/filetocreate.txt" or die "Can't open '$existingdir/filetocreate.txt'\n";
print $fileHandle "FooBar!\n";
close $fileHandle;
This should work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With