web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Getting syntax error o...
Power Automate
Unanswered

Getting syntax error or invalid value while running Run python script action in desktop flows

(0) ShareShare
ReportReport
Posted on by 2

this is the script I am running 

import datetime

# Get the current date
current_date = datetime.datetime.now()

# Initialize a counter
counter = 0

# Define the number of iterations
total_iterations = 730 # for 2 years

while counter < total_iterations:
# Calculate Date2 as Date1 - 15 days
date2 = current_date - datetime.timedelta(days=15)

# Print Date1 and Date2
print("Date1:", current_date.strftime("%Y-%m-%d"))
print("Date2:", date2.strftime("%Y-%m-%d"))

# Update Date1 for the next iteration
current_date = date2 - datetime.timedelta(days=1)

# Increment the counter
counter += 1

I have provide correct alligment to the script. Can someone help regarding this.

I have the same question (0)
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    The syntax error is because of the percentage symbols. These are reserved by PAD for variable notation. To pass a literal % to the script, you need to escape it with another %. Try this:

    print("Date1:", current_date.strftime("%%Y-%%m-%%d"))
    print("Date2:", date2.strftime("%%Y-%%m-%%d"))

     

    Please note, however, that PAD can only return a single value from a Python script. When you have several print statements, you will not get the actual 730 x 2 strings printed out. If you want that, you should in fact append your values to a single string inside the loop and then use a single print statement at the end of the script.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Abhisht Profile Picture
    2 on at

    its working but its showing empty in the display message 

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Can you share a screenshot of your flow?

  • Srini007 Profile Picture
    3,327 Super User 2025 Season 2 on at

    Hi @Abhisht 

     

    Just set the indentation as below

     

    import datetime
    
    # Get the current date
    current_date = datetime.datetime.now()
    
    # Initialize a counter
    counter = 0
    
    # Define the number of iterations
    total_iterations = 730 # for 2 years
    
    while counter < total_iterations:
     # Calculate Date2 as Date1 - 15 days
     date2 = current_date - datetime.timedelta(days=15)
    
     # Print Date1 and Date2
     print "Date1:", current_date.strftime("%%Y-%%m-%%d")
     print "Date2:", date2.strftime("%%Y-%%m-%%d")
    
     # Update Date1 for the next iteration
     current_date = date2 - datetime.timedelta(days=1)
    
     # Increment the counter
     counter += 1
    

     

    Try as above and you are fine to get the results

     

    Srini84_0-1697612179845.png

     

    If you find this reply helpful, please consider giving it a LIKE AND

    If this reply answers your question or solves your issue, please ACCEPT AS SOLUTION

     

    Thanks,

    Srini

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard