juliolop
Posts: 20
Joined: Thu Sep 27, 2018 4:33 pm

trying to rename a file

Fri Dec 28, 2018 5:51 pm

in every run
I'm trying to rename a file my code always generates in every session in the ending_session function, named report.rep
that name will be replaced with the specific session's WO number to keep records.
what am I doing wrong here?
import os

def ending_session():
CDU.destroy() #close windows
os.rename ('report.rep', %s.rep' % (UUT_wo.get())) #
sys.exit()

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: trying to rename a file

Fri Dec 28, 2018 6:11 pm

Are you sure that the file is in the working directory (looking from your Python script).
If not then you may have to use the full path.
Do you get any error messages?

BTW: put any code in code tags; essential when posting Python code.

juliolop
Posts: 20
Joined: Thu Sep 27, 2018 4:33 pm

Re: trying to rename a file

Fri Dec 28, 2018 6:19 pm

yes I'm sure they're both in the working directory
no errors
the problem is the filename should be the work order number with extension .rep
and the name is completely different
for example
I run a session with WO number 1002222
the file name after be renamed the report.rep file should be 1002222.rep
the one I get is .1974809936.rep

if I open it I can see in the report the WO:1002222
but not in the file name

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: trying to rename a file

Sat Dec 29, 2018 12:18 am

juliolop wrote:
Fri Dec 28, 2018 5:51 pm
in every run
I'm trying to rename a file my code always generates in every session in the ending_session function, named report.rep
that name will be replaced with the specific session's WO number to keep records.
what am I doing wrong here?
import os

def ending_session():
CDU.destroy() #close windows
os.rename ('report.rep', %s.rep' % (UUT_wo.get())) #
sys.exit()
What is UUT_wo? What gets printed if you put the following line between the os.rename() and sys.exit() lines? It should be the string that your filename is renamed to sans the extension, if this isn't the string you are expecting then it sounds like UUT_wo contains something other than what you think it should.

Code: Select all

print('UUT_wo.get() returned "{}"'.format(UUT_wo.get()))
Your os.rename() has a missing quote at the start of the second argument, I take it that is a typo, it's better to cut-and-paste the actual code to avoid typing it in here different to what you are running.
She who travels light — forgot something.

juliolop
Posts: 20
Joined: Thu Sep 27, 2018 4:33 pm

Re: trying to rename a file

Wed Jan 02, 2019 1:20 pm

first of all Happy New Year
and thanks again for all your support
this line you gave me causes an "unexpected indent" error

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: trying to rename a file

Wed Jan 02, 2019 2:28 pm

juliolop wrote:
Wed Jan 02, 2019 1:20 pm
first of all Happy New Year
and thanks again for all your support
this line you gave me causes an "unexpected indent" error
Python is heavily dependent on indentation, you have to indent that line so that it matches the line before it so that it belongs to the same block of code. You failed to have any indentation in your original post, nor did you use code tags around the code which would prevent the forum from stripping any leading indentation, so I couldn't include indentation being as I have absolutely no idea of how much is needed in your case. I assumed you knew enough about Python's indentation rules to correctly indent it yourself.
She who travels light — forgot something.

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: trying to rename a file

Wed Jan 02, 2019 2:30 pm

juliolop wrote:
Wed Jan 02, 2019 1:20 pm
first of all Happy New Year
and thanks again for all your support
this line you gave me causes an "unexpected indent" error
Well, you need to get the indentation the same as the around it.
In Python the indentation is an essential part of the language. Make sure you use just spaces (not tabs).
That's why it's also important that you use code tags when posting python code on this forum (viewtopic.php?f=32&t=84477)

It would look something like this

Code: Select all

def ending_session():
    CDU.destroy() #close windows
    os.rename ('report.rep', %s.rep' % (UUT_wo.get())) #
    print('UUT_wo.get() returned "{}"'.format(UUT_wo.get()))
    sys.exit()

juliolop
Posts: 20
Joined: Thu Sep 27, 2018 4:33 pm

Re: trying to rename a file

Wed Jan 02, 2019 3:13 pm

meanwhile I continued working the code and achieved with this

Code: Select all

   def ending_session():
   print("Work Order: %s\n" % (UUT_wo.get()))
   os.rename('report.rep','%s.rep' % (UUT_wo.get()))
   CDU.destroy()    
   sys.exit()
is it ok?

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: trying to rename a file

Wed Jan 02, 2019 3:43 pm

So whatever UUT_wo is I suspect it has a dependency on CDU and calling CDU.destroy() first left UUT_wo with garbage data.

It would have helped if you'd have told us what these things are and how they are related, there was nothing in what you posted that even hinted at a connection between these two unknown objects.
She who travels light — forgot something.


User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: trying to rename a file

Wed Jan 02, 2019 6:40 pm

gordon77 wrote:
Wed Jan 02, 2019 4:11 pm
see this thread... viewtopic.php?f=32&t=229158&p=1410886#p1410886
Well juliolop never referenced that thread in this one and I hadn't read that thread before. It's a bit much for a poster to expect that anybody else either knows what has been posted in other threads or that they will have the time or inclination to go searching for information that the poster should have given. For all anybody knows this thread could've been about a totally different program from the one in that thread.
She who travels light — forgot something.

juliolop
Posts: 20
Joined: Thu Sep 27, 2018 4:33 pm

Re: trying to rename a file

Thu Jan 03, 2019 2:39 pm

gordon77 wrote:
Wed Jan 02, 2019 4:11 pm
see this thread... viewtopic.php?f=32&t=229158&p=1410886#p1410886
yes Gordon I think the same,
thanks for everything

Return to “Python”