Skip to main content
Solved

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


Forum|alt.badge.img

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
View original
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, 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
Contributor
Forum|alt.badge.img+19
  • Contributor
  • 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
Evangelist
  • February 1, 2016

Hi

Try something like this:

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

David


Forum|alt.badge.img
  • Author
  • 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
Evangelist
  • February 1, 2016
michaels wrote:

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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings