Wroof has asked for the wisdom of the Perl Monks concerning the following question:
Good morning Monks,
Type tool meaning. I am trying to write up a short test program to learn how to deal with files in Perl before I try and implement it into a program I have already written.
The idea of the test program is to open a file for appending and if the file does not exist create it and open it. The software in the end will be used to create a log in another program to help me track down an error that is occurring around once every 40-50 days.
What I have at the moment is just trying to open a file for appending/create the file, and then close the file before testing that it exists. This is all based on the IO:File Lib. The function $fh->open('>> test2.txt') always seams to return true even if the files does not exist and it does not create it either.
Tests whether a file exists. Parameters: path - the path to the file to test options - options indicating how symbolic links are handled. Returns: true if the file exists; false if the file does not exist or its existence cannot be determined. To simply check for a file’s existence, we can use exists and notExists method of java.nio.file.Files class. The exists method returns true if the file exists, whereas the notExists method returns true when it does not exist.
Java Create File If Not Exists
|
---|
Java Create File If Not Exists In Excel
Replies are listed 'Best First'. | |
---|---|
Re: Check if a file exists and if not create it by GotToBTru (Prior) on Aug 20, 2014 at 21:05 UTC |
|
Is there a reason to use IO::File instead of open $fh,'>>',$filename;?
Output:
|
[reply] [d/l] [select] |
Re^2: Check if a file exists and if not create it
by Wroof (Novice) on Aug 20, 2014 at 21:26 UTC |
|
hey thanks for this. there was no reason I was using IO:File apart from what I found online in a tutorial. I found the problem was with where the program was creating the file 'C:Usersuser' instead of that I expected it to create it in the same directory the program is in.
|
[reply] |
Re: Check if a file exists and if not create it by Your Mother (Archbishop) on Aug 20, 2014 at 21:50 UTC |
|
Ubuntu small iso. Learning is great. There are tons of modern packages that make this stuff really easy. Path::Tiny, for example, saved me a lot of headache just yesterday in a one-liners renaming a bunch of wget files that had query strings in the file names and couldn't be shared with a Windows user who needed them… I digress. One of many ways to do your task–
perl -MPath::Tiny -le 'path('ohai')->touch'
|
[reply] [d/l] [select] |
Re^2: Check if a file exists and if not create it
by Your Mother (Archbishop) on Aug 22, 2014 at 01:52 UTC |
|
Gonna reply to myself while I remember the digression because it was one of those MY LORD, I LOVE PERL moments–
find . -type f | perl -MPath::Tiny -lne '/^(.+?)?/ && path($_)->move($1)'
Finds names like /this/was-not-a/good.aspx?idea=for&a%20filename and turns them into /this/was-not-a/good.aspx.
|
[reply] [d/l] [select] |
Re: Check if a file exists and if not create it by Laurent_R (Canon) on Aug 20, 2014 at 21:11 UTC |
|
Surprising. Opening a file in '>>' mode should create the file if it does not exist. And it does. Just opening the file without even doing anything to it (not trying to write anything) creates an empty file: is sufficient to create the bar.txt file:
|
[reply] [d/l] [select] |
Re: Check if a file exists and if not create it - use sysopen by Discipulus (Abbot) on Aug 21, 2014 at 11:56 UTC |
|
Hello Wroof and welcome,
consider that there are many ways to do it in Perl.. Normally a Perl programmer after years of using open meet also his father sysopen to have more granularity on existing or not file. HtH L* UPDATE: some docs are missing
There are no rules, there are no thumbs.
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS. |
[reply] [d/l] |
Re^2: Check if a file exists and if not create it - use sysopen
by t-rex (Scribe) on Jun 24, 2016 at 08:34 UTC |
|
i get bare word error when i complie the script using flags O_RDWR or others, what could be the reason ?
|
[reply] |
Re^3: Check if a file exists and if not create it - use sysopen
by Discipulus (Abbot) on Jun 24, 2016 at 08:57 UTC |
|
have you imported the module with use Fcntl ? see Fcntl
L*
There are no rules, there are no thumbs.
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS. |
[reply] [d/l] [select] |
Re: Check if a file exists and if not create it by Aldebaran (Curate) on Aug 21, 2014 at 21:59 UTC |
|
I believe that checking to see whether a file exists before creating it can create a race condition. I've been told that the better way to do this is to create the file, and deal with the exception thrown if the file already exists.
|
[reply] |