askopenfilename in windows and linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • barrowman
    New Member
    • Feb 2016
    • 5

    askopenfilename in windows and linux

    Hi,
    In linux I wrote a script which uses askopenfilename which I use to copy files from one place to another. Python 2.7.6
    I copied the script to a windows 10 PC and, when I had sorted out various bits of syntax it runs under idle except for one thing. Python 2.7.9
    Doing a print shows the folder it's coming from has the linux syntax:
    E:/Test/filetocopy instead of E:\Test\filetoc opy and I think this is why it fails.
    I can manually create folders and copy files there so it isn't a permissions problem as far as I can see.
    There seem to be no error messages
    Any idea what's wrong please?
    Cannot find out why this is happening but I have found a workaround using
    Code:
    (filepath, filename) = os.path.split(myname)
    and reconstructing the full path by using the filename from the above
    Last edited by barrowman; Feb 26 '16, 03:57 PM. Reason: more added information
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You didn't say what "it fails" means or what "Cannot find out why this is happening".

    Python employs the common usage (backslash is only used on Windows and can be confusing because it starts an escape character) as a standard on all systems. It then converts to the appropriate symbol for each OS when necessary. The same is true for newlines. Python uses "/n". It is converted to "\r" for Mac and "\r\n" for Windows (at least I think that's what they are). I am guessing, but if you have a single backslash in the path it is being interpreted as an escape character which distorts the path name, so \some_dir\new_d ir would see the "\n" as a newline. Can't really help without code though.

    Comment

    • barrowman
      New Member
      • Feb 2016
      • 5

      #3
      Thanks dwblas. I realized that when I did the workaround. Although the original Linux program was all in a nice gui this windows one is quite crude but I thought i would include the source code of my solution in case anyone actually finds it usefull.
      Code:
      #!\c:\Python27/pythonw
      from Tkinter import *
      import tkFileDialog
      import tkMessageBox
      import os
      import time
      from ctypes import windll, byref, wintypes
      from ctypes.wintypes import SMALL_RECT
      
      
      root = Tk()
      STDOUT = -12
      
      todir = "c:\ProgramData\SoundBox\Videos"
      #Pick a video file and copy it.
      def select_video():
          myname=tkFileDialog.askopenfilename(initialdir="c:\\")
          if myname =="":
                 return()
          else:
                  (filepath, filename) = os.path.split(myname)
                  myfin = filename
                  fo = filepath.replace("/", "\\")
                  fo = fo + "\\" + myfin            
                  doit="copy "  + fo + " " + todir
                  quit()
      hdl = windll.kernel32.GetStdHandle(STDOUT)
      rect = wintypes.SMALL_RECT(0, 50, 50, 30) # (left, top, right, bottom)
      windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
      select_video()
      
      root.mainloop()

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        I'm on Linux so don't know much about this, but apparently "\anything" doesn't so what you might think, at least on my Slackware box.
        Code:
         repr("a\P\S")
        "'a\\\\P\\\\S'"
        Get in the habit of using a forward slash or double backslash and avoid those problems.
        Code:
        todir = "c:\ProgramData\SoundBox\Videos"

        Comment

        Working...