Skip to main content
Solved

Startup Python script: How can I create an empty file?

  • February 1, 2016
  • 4 replies
  • 55 views

michaels
Contributor
Forum|alt.badge.img+5

Hi

how can I create an empty txt-file with the python startup script?

That file has to be into the same directory, where the data in the workflow will writen to. So I will need the "DestDataset_SHAPE" as the directory and "LTYP.mif" as the filename.

Thanks very much for your support.

Best answer by takashi

An example.

# Startup Python Script Example
import fme, os

# Get the destination folder path.
dir = fme.macroValues['DestDataset_SHAPE']

# Create the folder if it doesn't exist.
if not os.path.exists(dir):
    os.makedirs(dir)
    
# Create an empty file.
# Open a file with writing mode, and then just close it.
with open(os.path.join(dir, 'LTYP.mif'), 'w'):
    pass
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

takashi
Celebrity
  • Best Answer
  • February 1, 2016

An example.

# Startup Python Script Example
import fme, os

# Get the destination folder path.
dir = fme.macroValues['DestDataset_SHAPE']

# Create the folder if it doesn't exist.
if not os.path.exists(dir):
    os.makedirs(dir)
    
# Create an empty file.
# Open a file with writing mode, and then just close it.
with open(os.path.join(dir, 'LTYP.mif'), 'w'):
    pass

david_r
Celebrity
  • February 1, 2016

Hi

Try something like this:

shape_dir = FME_MacroValues['DestDataset_SHAPE']
filename = shape_dir + '/LTYP.mif'
open(filename, 'w').close()

David


michaels
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • February 1, 2016

Thanks for this answers. I tried both, but I get allways this error message:

|ERROR |Python Exception <IndentationError>: unexpected indent (<string>, line 2)

|ERROR |Error executing string ` # Startup Python Script Example

import fme, os

# Get the destination folder path.

dir = fme.macroValues['DestDataset_SHAPE']

# Create the folder if it doesn't exist.

if not os.path.exists(dir):

os.makedirs(dir)

# Create an empty file.

# Open a file with writing mode, and then just close it.

with open(os.path.join(dir, 'LTYP.mif'), 'w'):

pass'

|ERROR |FME_BEGIN_PYTHON failed to execute provided script

What could be the problem here?


david_r
Celebrity
  • February 1, 2016

Thanks for this answers. I tried both, but I get allways this error message:

|ERROR |Python Exception <IndentationError>: unexpected indent (<string>, line 2)

|ERROR |Error executing string ` # Startup Python Script Example

import fme, os

# Get the destination folder path.

dir = fme.macroValues['DestDataset_SHAPE']

# Create the folder if it doesn't exist.

if not os.path.exists(dir):

os.makedirs(dir)

# Create an empty file.

# Open a file with writing mode, and then just close it.

with open(os.path.join(dir, 'LTYP.mif'), 'w'):

pass'

|ERROR |FME_BEGIN_PYTHON failed to execute provided script

What could be the problem here?

Identations are meaningful in Python, you will have to make sure you match the exact same indents as used by Takashi. Otherwise you will get this error.