mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Create file without writing to it?

Sat Nov 07, 2015 2:43 pm

When my program runs for the first time I simply want to CREATE the file but not write anything to it. (filename.dat)

Later on in the program I will be writing and saving the data and that works fine but having a problem with "file doesn't exist" on the first run of the program and I can't figure this out.

Want to create the file the first time I run the program and then I suppose have it check (although don't really need that I guess) to see if it exists but WITHOUT altering the file in any way. -- thanks much for any help

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: Create file without writing to it?

Sat Nov 07, 2015 3:28 pm

Have a look here, maybe this solves your problem. ;)

Laurens

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Create file without writing to it?

Sat Nov 07, 2015 8:12 pm

Isn't there a simple make directory command? Been trying all afternoon to solve this.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Create file without writing to it?

Sat Nov 07, 2015 8:20 pm

That's a different question to your original one.

For directories, there's this: https://docs.python.org/2/library/os.html#os.mkdir

Edit: and see this http://stackoverflow.com/questions/2731 ... -necessary
Last edited by elParaguayo on Sat Nov 07, 2015 8:28 pm, edited 1 time in total.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Create file without writing to it?

Sat Nov 07, 2015 8:22 pm

To create a new directory use mkdir
To create a zero byte file (empty) you can use the "touch" command. touch filename.dat
The "touch" command will also update the file modification time whether the file contains any data or not.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Create file without writing to it?

Sat Nov 07, 2015 8:32 pm

stupid me -- os.makedir ? -- i'll try that and just have it run once

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Create file without writing to it?

Sat Nov 07, 2015 8:32 pm

JimmyN wrote:To create a new directory use mkdir
To create a zero byte file (empty) you can use the "touch" command. touch filename.dat
The "touch" command will also update the file modification time whether the file contains any data or not.
If you haven't noticed this post is in the Python section...
There are 10 types of people: those who understand binary and those who don't.

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Create file without writing to it?

Sat Nov 07, 2015 8:39 pm

mmkw43 wrote:When my program runs for the first time I simply want to CREATE the file but not write anything to it. (filename.dat)

Later on in the program I will be writing and saving the data and that works fine but having a problem with "file doesn't exist" on the first run of the program and I can't figure this out.

Want to create the file the first time I run the program and then I suppose have it check (although don't really need that I guess) to see if it exists but WITHOUT altering the file in any way. -- thanks much for any help
What are you trying to do when it says it doesn't exist, reading it?

Why not only read it if it exists?

Code: Select all

   if os.path.exists("filename.dat") == True:
      file = open("filename.dat","r")
      inputx = file.readline()  

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Create file without writing to it?

Sat Nov 07, 2015 9:30 pm

How about

Code: Select all

import os
def touch(path):
    with open(path, 'a'):
        os.utime(path, None)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Create file without writing to it?

Sat Nov 07, 2015 9:33 pm

I need to create the file (not folder -- so mkdir was my mistake) before I read it. I keep getting errors that the file doesn't exist (duh) when I try to read it. So I simply want to create an empty file myfile.dat when the program starts -- if it exists then it's ignored. Essentially creating the file(s) the first time the program is run.

I'm using pickle to read the file which I want to write in binary. But like I say, it needs to be created first in my python program.

This is working fine but can't get eml.dat to create --

Code: Select all

if not os.path.exists('/home/pi/eml.dat'):   #this line works fine
   what here ? (tried the suggestions -- no luck)

(later on in the program in a class I read the file as such  ---  einput = open('eml.dat', 'rb')
thanks

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Create file without writing to it?

Sat Nov 07, 2015 9:33 pm

Try...

open('filename.dat', 'w').close()
Last edited by gordon77 on Sat Nov 07, 2015 9:47 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Create file without writing to it?

Sat Nov 07, 2015 9:36 pm

mmkw43 wrote:

Code: Select all

if not os.path.exists('/home/pi/eml.dat'):   #this line works fine
   what here ? (tried the suggestions -- no luck)

(later on in the program in a class I read the file as such  ---  einput = open('eml.dat', 'rb')
See above ^^^^

Code: Select all

path='/home/pi/eml.dat'
if not os.path.exists(path):   #this line works fine
    with open(path, 'a'):
        os.utime(path, None)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Create file without writing to it?

Sat Nov 07, 2015 11:32 pm

thanks friends.

Return to “Python”