Forum Discussion

Amrendra's avatar
Amrendra
Occasional Contributor
7 years ago
Solved

i want to remove a file from my desktop .

i want to remove a file name (export.csv) from my desktop . I have code in python working fine for this.

 

Code :-

try:

     import os

     os.remove(r"C:\Users\zkhz8zd\Desktop\export.csv")

   except Exception as e:

     Log.Message(str(e)

 

Now I want it to make generic so that whoever run on there respective machine, it should work. I am using

Sys.UserName for this, however not able to do this.

 

I have changed ....os.remove(r"C:\Users\zkhz8zd\Desktop\export.csv") to os.remove(r"C:\Users\

+ str(Sys.UserName) +

\Desktop\export.csv") 

 

Can anybody please help me with the code

 

  • Hi Amrendra,

     

    You can get the desktop folder path as follows:

    # TC 12+
    desktop = WshShell.SpecialFolders.Item('Desktop')

    # TC 11.x
    desktop = Sys.OleObject['WScript.Shell'].SpecialFolders.Item('Desktop')

    This will work even on non-English versions of Windows and if it's installed on drives other than C:.

     

    Then you can use os.path.join() to get the full file name:

    os.remove( os.path.join(desktop, 'export.csv') )

     

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Amrendra,

     

    You can get the desktop folder path as follows:

    # TC 12+
    desktop = WshShell.SpecialFolders.Item('Desktop')

    # TC 11.x
    desktop = Sys.OleObject['WScript.Shell'].SpecialFolders.Item('Desktop')

    This will work even on non-English versions of Windows and if it's installed on drives other than C:.

     

    Then you can use os.path.join() to get the full file name:

    os.remove( os.path.join(desktop, 'export.csv') )

     

  • m_essaid's avatar
    m_essaid
    Valued Contributor

    hi,

    I don't know python but do it in 2 times.

    first initalize a variable with the path, then use this variable.

    put a debug point just after the initialization and copy-paste in an explorer window, to see if the path is correct.

    • Amrendra's avatar
      Amrendra
      Occasional Contributor

      i have tried :-

       

      def a():

      userName = Sys.UserName

      Log.Message(userName)

      try:

      import os

      os.remove(r"C:\Users\userName\Desktop\export.csv")

      except Exception as e:

      Log.Message(str(e))

       

      However this is giving me error message " [WinError 3] The system cannot find the path specified: 'C:/Users/userName/Desktop/export.csv'

       

      Please help

  • Amrendra's avatar
    Amrendra
    Occasional Contributor

    zkhz8zd is mu login id in below code and I want to make it generic for the other user who looged with there respective id.

    Please help me out. Pls