Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using belwo Python code to open current Directory.
However I am passing Directory as a Variable, but it is not opening. It opens default documetns folder

Python
current_date = datetime.datetime.today().strftime('%d-%B-%Y')
subprocess.Popen('explorer "D:\\SBC\\01-03IRE\\15-Daily_Count\\"+current_date+"\\"')


What I have tried:

Python
current_date = datetime.datetime.today().strftime('%d-%B-%Y')
subprocess.Popen('explorer "D:\\SBC\\01-03IRE\\15-Daily_Count\\"+current_date+"\\"')
Posted
Updated 14-Sep-21 2:41am
v2

1 solution

Your quote characters are wrong as shown when I print out the string:
explorer "D:\SBC\01-03IRE\15-Daily_Count\"+current_date+"\"

Change the double quotes around current_date to single, thus:
Python
current_date = datetime.datetime.today().strftime('%d-%B-%Y')
subprocess.Popen('explorer "D:\\SBC\\01-03IRE\\15-Daily_Count\\' +current_date+ '\\"')
                                                               ^                ^
 
Share this answer
 
v3
Comments
Sandeep Chavan 2021 14-Sep-21 10:58am    
Hi Richard,
Thank you very much, its working.

One more related to this
Below code is not working, please suggest

Daily_Files = "D:\\SBC\\01-03IRE\\11-Daily-Files\\"
subprocess.Popen('explorer "Daily_Files"')
Richard MacCutchan 14-Sep-21 11:09am    
You are trying to pass the string "Daily_Files". You need to use the same syntax as in your first example:
subprocess.Popen('explorer ' +Daily_Files+ '\\')

Take a look at 7. Input and Output — Python 3.9.7 documentation[^] for a more comprehensive explanation of how to build strings from a mixture of text and variables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900