By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,255 Members | 2,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,255 IT Pros & Developers. It's quick & easy.

askopenfilename in windows and linux

P: 5
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\filetocopy 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
Expand|Select|Wrap|Line Numbers
  1. (filepath, filename) = os.path.split(myname)
and reconstructing the full path by using the filename from the above
Feb 26 '16 #1

✓ answered by dwblas

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.
Expand|Select|Wrap|Line Numbers
  1.  repr("a\P\S")
  2. "'a\\\\P\\\\S'" 
Get in the habit of using a forward slash or double backslash and avoid those problems.
Expand|Select|Wrap|Line Numbers
  1. todir = "c:\ProgramData\SoundBox\Videos"
  2.  

Share this Question
Share on Google+
3 Replies


Expert 100+
P: 621
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_dir would see the "\n" as a newline. Can't really help without code though.
Feb 26 '16 #2

P: 5
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.
Expand|Select|Wrap|Line Numbers
  1. #!\c:\Python27/pythonw
  2. from Tkinter import *
  3. import tkFileDialog
  4. import tkMessageBox
  5. import os
  6. import time
  7. from ctypes import windll, byref, wintypes
  8. from ctypes.wintypes import SMALL_RECT
  9.  
  10.  
  11. root = Tk()
  12. STDOUT = -12
  13.  
  14. todir = "c:\ProgramData\SoundBox\Videos"
  15. #Pick a video file and copy it.
  16. def select_video():
  17.     myname=tkFileDialog.askopenfilename(initialdir="c:\\")
  18.     if myname =="":
  19.            return()
  20.     else:
  21.             (filepath, filename) = os.path.split(myname)
  22.             myfin = filename
  23.             fo = filepath.replace("/", "\\")
  24.             fo = fo + "\\" + myfin            
  25.             doit="copy "  + fo + " " + todir
  26.             quit()
  27. hdl = windll.kernel32.GetStdHandle(STDOUT)
  28. rect = wintypes.SMALL_RECT(0, 50, 50, 30) # (left, top, right, bottom)
  29. windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
  30. select_video()
  31.  
  32. root.mainloop()
Feb 26 '16 #3

Expert 100+
P: 621
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.
Expand|Select|Wrap|Line Numbers
  1.  repr("a\P\S")
  2. "'a\\\\P\\\\S'" 
Get in the habit of using a forward slash or double backslash and avoid those problems.
Expand|Select|Wrap|Line Numbers
  1. todir = "c:\ProgramData\SoundBox\Videos"
  2.  
Feb 26 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Browse more Python Questions on Bytes