Re: Best Practice Question
- Original Message - [...] By the way, did someone ever notice that r'\' fails ? I'm sure there's a reason for that... (python 2.5) Anyone knows ? r'\' SyntaxError: EOL while scanning single-quoted string Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r\ is a valid string literal consisting of two characters: a backslash and a double quote; r\ is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). -- python docs -- http://mail.python.org/mailman/listinfo/python-list Thanks for the pointer. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Practice Question
On 02/04/2013 11:23 PM, Anthony Correia wrote: Just started learning Python. I just wrote a simple copy files script. I use Powershell now as my main scripting language but I wanted to extend into the linux platform as well. Is this the best way to do it? import os objdir = (C:\\temp2) colDir = os.listdir(objdir) for f in colDir: activefile = os.path.join(objdir + \\ + f) print (Removing + activefile + from + objdir) os.remove(activefile) In Powershell I would do this: $colDir = gci -path c:\temp2 $objDir = C:\temp3 ForEach($file in $colDir){ #.Fullname lists the directory and filename together. No need to do a join #beforehand. Copy-item $file.fullname -destination $objDir } You started two nearly-identical threads, with nearly the same content. I won't repeat the comments already posted in the other thread, but notice that your powershell script copies the file, while your Python translation deletes the file. Big difference. Next, you should use raw strings, or at least use the forward slash, rather than double backslashes in file path literals. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Practice Question
- Original Message - On 02/04/2013 11:23 PM, Anthony Correia wrote: Just started learning Python. I just wrote a simple copy files script. I use Powershell now as my main scripting language but I wanted to extend into the linux platform as well. Is this the best way to do it? import os objdir = (C:\\temp2) colDir = os.listdir(objdir) for f in colDir: activefile = os.path.join(objdir + \\ + f) print (Removing + activefile + from + objdir) os.remove(activefile) In Powershell I would do this: $colDir = gci -path c:\temp2 $objDir = C:\temp3 ForEach($file in $colDir){ #.Fullname lists the directory and filename together. No need to do a join #beforehand. Copy-item $file.fullname -destination $objDir } You started two nearly-identical threads, with nearly the same content. I won't repeat the comments already posted in the other thread, but notice that your powershell script copies the file, while your Python translation deletes the file. Big difference. Next, you should use raw strings, or at least use the forward slash, rather than double backslashes in file path literals. -- DaveA He must have hit the send button too early by mistake. By the way, did someone ever notice that r'\' fails ? I'm sure there's a reason for that... (python 2.5) Anyone knows ? r'\' SyntaxError: EOL while scanning single-quoted string r'\b' '\\b' JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Practice Question
[...] By the way, did someone ever notice that r'\' fails ? I'm sure there's a reason for that... (python 2.5) Anyone knows ? r'\' SyntaxError: EOL while scanning single-quoted string Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r\ is a valid string literal consisting of two characters: a backslash and a double quote; r\ is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). -- python docs -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Practice Question
On Tue, 5 Feb 2013 15:32:32 +0100 (CET), Jean-Michel Pichavant wrote: By the way, did someone ever notice that r'\' fails ? I'm sure there's a reason for that... (python 2.5) Anyone knows ? r'\' SyntaxError: EOL while scanning single-quoted string I hit this all the time with Vim's path-completion (:help i_CTRL-X_CTRL-F) on Win32 which puts a trailing \ on directory-names. I just need to remember to remove it, a task made easier because the syntax highlighting correctly shows how Python interprets it (i.e., the string is still continued). -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Practice Question
On Tuesday, February 5, 2013 10:17:54 AM UTC-5, pytho...@tim.thechases.com wrote: On Tue, 5 Feb 2013 15:32:32 +0100 (CET), Jean-Michel Pichavant wrote: By the way, did someone ever notice that r'\' fails ? I'm sure there's a reason for that... (python 2.5) Anyone knows ? r'\' SyntaxError: EOL while scanning single-quoted string I hit this all the time with Vim's path-completion (:help i_CTRL-X_CTRL-F) on Win32 which puts a trailing \ on directory-names. I just need to remember to remove it, a task made easier because the syntax highlighting correctly shows how Python interprets it (i.e., the string is still continued). -tkc Sorry about that I hit the touchpad on my laptop by mistake. Beside the using single '\' vs a double '\\' does that look ok? -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Practice Question
On Tue, Feb 5, 2013 at 11:40 AM, Anthony Correia akcorr...@gmail.comwrote: On Tuesday, February 5, 2013 10:17:54 AM UTC-5, pytho...@tim.thechases.comwrote: On Tue, 5 Feb 2013 15:32:32 +0100 (CET), Jean-Michel Pichavant wrote: By the way, did someone ever notice that r'\' fails ? I'm sure there's a reason for that... (python 2.5) Anyone knows ? r'\' SyntaxError: EOL while scanning single-quoted string I hit this all the time with Vim's path-completion (:help i_CTRL-X_CTRL-F) on Win32 which puts a trailing \ on directory-names. I just need to remember to remove it, a task made easier because the syntax highlighting correctly shows how Python interprets it (i.e., the string is still continued). -tkc Sorry about that I hit the touchpad on my laptop by mistake. Beside the using single '\' vs a double '\\' does that look ok? -- http://mail.python.org/mailman/listinfo/python-list according to the docs for os.path.join, you don't need the backslash stuff at all. Python knows the correct separator for your os and inserts it accordingling: I'm on linux: import os p = os.path.join('bob', 'bill') p 'bob/bill' -- Joel Goldstick http://joelgoldstick.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Practice Question
On 02/05/2013 11:53 AM, Joel Goldstick wrote: On Tue, Feb 5, 2013 at 11:40 AM, Anthony Correia akcorr...@gmail.comwrote: On Tuesday, February 5, 2013 10:17:54 AM UTC-5, pytho...@tim.thechases.comwrote: On Tue, 5 Feb 2013 15:32:32 +0100 (CET), Jean-Michel Pichavant wrote: By the way, did someone ever notice that r'\' fails ? I'm sure there's a reason for that... (python 2.5) Anyone knows ? r'\' SyntaxError: EOL while scanning single-quoted string I hit this all the time with Vim's path-completion (:help i_CTRL-X_CTRL-F) on Win32 which puts a trailing \ on directory-names. I just need to remember to remove it, a task made easier because the syntax highlighting correctly shows how Python interprets it (i.e., the string is still continued). -tkc Sorry about that I hit the touchpad on my laptop by mistake. Beside the using single '\' vs a double '\\' does that look ok? -- http://mail.python.org/mailman/listinfo/python-list according to the docs for os.path.join, you don't need the backslash stuff at all. Python knows the correct separator for your os and inserts it accordingling: I'm on linux: import os p = os.path.join('bob', 'bill') p 'bob/bill' Worse than that, the code as posted by the OP used string concatenation before calling os.path.join(), and the latter method does nothing at all, when presented with a single string. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list