cannot print escape the escape character in python

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 761 Views - Last Post: 14 April 2018 - 05:26 PM Rate Topic: -----

#1 bobsmith76  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 159
  • Joined: 14-February 17

cannot print escape the escape character in python

Posted 06 April 2018 - 12:07 PM

I'm going to have to move a lot of files to my iCloud over an over again so I'm trying to automate the process. I can access the folder with command line but not python. It seems that the reason is because the path of the icloud folder uses a \ character.

I have tried

icloud = "~/Library/Mobile\\ Documents/com~apple~CloudDocs/iCloud~com~omz-software~Pythonista3/Documents/"



And also

icloud = "~/Library/Mobile\ Documents/com~apple~CloudDocs/iCloud~com~omz-software~Pythonista3/Documents/"



But both of them get translated into:

~/Library/Mobile\\ Documents/com~apple~CloudDocs/iCloud~com~omz-software~Pythonista3/Documents/


I cannot figure out how to prevent the double back slash from appearing.

Is This A Good Question/Topic? 0
  • +

Replies To: cannot print escape the escape character in python

#2 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 774
  • View blog
  • Posts: 3,170
  • Joined: 13-June 14

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 12:21 PM

What? If the directory just has a space in the name, the shell is just putting a slash there to escape it. You shouldn't need to include any slashes in the Python code:

$ mkdir some/"directory with spaces"
mkdir: created directory 'some'
mkdir: created directory 'some/directory with spaces'
$ touch some/directory\ with\ spaces/file
$ python3
Python 3.6.2 (default, Oct  5 2017, 12:21:44) 
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os 
>>> os.chdir("some/directory with spaces")
>>> os.listdir()
['file']
>>> 



This post has been edited by ndc85430: 06 April 2018 - 12:21 PM

Was This Post Helpful? 2
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11016
  • View blog
  • Posts: 18,800
  • Joined: 19-March 11

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 12:22 PM

You probably want to use os.path.join to combine your path elements, rather than trying to construct the right literal string.

https://docs.python....ml#os.path.join
Was This Post Helpful? 1
  • +
  • -

#4 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 774
  • View blog
  • Posts: 3,170
  • Joined: 13-June 14

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 12:22 PM

Or that, yes.
Was This Post Helpful? 0
  • +
  • -

#5 bobsmith76  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 159
  • Joined: 14-February 17

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 12:34 PM

Well, I can't get it. I don't understand what new code you want me to enter.

I should also add that in order to access the right file through command line I do have to do it in steps. So

~/Library/Mobile\ Documents/com~apple~CloudDocs/iCloud~com~omz-software~Pythonista3/Documents/

does not work but

cd ~/Library/Mobile\ Documents/
cd iCloud~com~omz-software~Pythonista3

does.
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 774
  • View blog
  • Posts: 3,170
  • Joined: 13-June 14

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 01:02 PM

I don't know what you're getting at. Those two seem to be two different paths.

Really, I'd just avoid creating directories with spaces in the names in the first place.
Was This Post Helpful? 1
  • +
  • -

#7 bobsmith76  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 159
  • Joined: 14-February 17

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 01:03 PM

I don't have any choice in the matter. Apple is the one that came up with this strange path. In order to store my files in icloud I have to use this path name.
Was This Post Helpful? 0
  • +
  • -

#8 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,263
  • Joined: 27-December 13

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 01:14 PM

A short while ago, I had a similar problem (reading from file, not the icloud thing): http://www.dreaminco...-python-script/

It was finally solved when I realized that you need forward slash only ('/') in file paths - never '\'.

Try to substitute all backward slashes to single forward ones.
Was This Post Helpful? 0
  • +
  • -

#9 bobsmith76  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 159
  • Joined: 14-February 17

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 01:28 PM

no, that didn't do it.
Was This Post Helpful? 0
  • +
  • -

#10 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,263
  • Joined: 27-December 13

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 01:30 PM

In what way does it not work ?
Do you get an error message ?
Can you show some of the relevant code ?
Was This Post Helpful? 0
  • +
  • -

#11 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,263
  • Joined: 27-December 13

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 01:43 PM

I guess that python is not able to recognize the tilde ('~') as a valid path separator.
I would try to substitute them as well, to '/'.
At least the very first one; but probably all (or maybe up to '/iCloud..').
Was This Post Helpful? 0
  • +
  • -

#12 bobsmith76  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 159
  • Joined: 14-February 17

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 03:02 PM

View PostDK3250, on 06 April 2018 - 01:43 PM, said:

I guess that python is not able to recognize the tilde ('~') as a valid path separator.
I would try to substitute them as well, to '/'.
At least the very first one; but probably all (or maybe up to '/iCloud..').



Here's the code I'm using. The following works:

import os
old_directory = "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/"
assert os.path.exists(old_directory)



The following codes raise an assertion error:

icloud = "/Users/kylefoley/Library/Mobile/ Documents/com/apple/CloudDocs/iCloud/com/omz-software/Pythonista3/Documents/"
icloud = "/Users/kylefoley/Library/Mobile\"
icloud = "/Users/kylefoley/Library/Mobile\\"
icloud = "~/Library/Mobile\\"
icloud = "/Library/Mobile\\"
icloud = "/Library/Mobile\"
icloud = "/Library/Mobile/"



Is there anyway I can get python to list the contents of a directory like command line does? Maybe if I could list the contents of a directory I could find out how python spells the directories.

This post has been edited by bobsmith76: 06 April 2018 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2575
  • View blog
  • Posts: 4,109
  • Joined: 21-June 11

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 03:16 PM

View PostDK3250, on 06 April 2018 - 10:14 PM, said:

Try to substitute all backward slashes to single forward ones.


He's not trying to use the backslashes as a directory separator. He's trying to use them to escape the spaces, which isn't necessary. The name of the directory is "Mobile Documents"; "Mobile /Documents" or "Mobile/Documents" would mean that there are two directories - one named "Mobile" (or "Mobile ") and one named "Documents" - which isn't the case.

View PostDK3250, on 06 April 2018 - 10:43 PM, said:

I guess that python is not able to recognize the tilde ('~') as a valid path separator.
I would try to substitute them as well, to '/'.


The tildes seem to be just a part of the name and should thus stay as-is.

Ndc85430 gave the correct answer right at the beginning. Just don't include the backslashes - that's all there is to it.

View Postbobsmith76, on 07 April 2018 - 12:02 AM, said:

Is there anyway I can get python to list the contents of a directory like command line does?


os.listdir.

This post has been edited by sepp2k: 06 April 2018 - 03:15 PM

Was This Post Helpful? 3
  • +
  • -

#14 bobsmith76  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 159
  • Joined: 14-February 17

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 03:42 PM

View Postsepp2k, on 06 April 2018 - 03:16 PM, said:

Ndc85430 gave the correct answer right at the beginning. Just don't include the backslashes - that's all there is to it.


That doesn't work. I've already tried

icloud = "/Users/kylefoley/Library/Mobile"
icloud = "/Users/kylefoley/Library/Mobile/"
icloud = "/Library/Mobile\\"
icloud = "/Library/Mobile/"



And none of them work. Also these are the ones that at least work with command line so they are the most promising. I've tried the following and they don't work.


#icloud = "/Library/Mobile\ Documents/com~apple~CloudDoc"
#icloud = "/Library/Mobile/ Documents/com~apple~CloudDoc"
#"/Library/Mobile/Documents/com/apple/CloudDoc"



Surely, someone here as an iPhone. Can't they try it?
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2575
  • View blog
  • Posts: 4,109
  • Joined: 21-June 11

Re: cannot print escape the escape character in python

Posted 06 April 2018 - 03:47 PM

View Postbobsmith76, on 07 April 2018 - 12:42 AM, said:

I've already tried

icloud = "/Users/kylefoley/Library/Mobile"
icloud = "/Users/kylefoley/Library/Mobile/"
icloud = "/Library/Mobile\\"
icloud = "/Library/Mobile/"



And none of them work.


Of course not. As I said, the name of the directory is "Mobile Documents". That's one directory with a space in its name. Not two directories where one is inside the other.

So you won't find any directory named "Mobile" because that's just one part of the name. The directory is called "Mobile Documents".

Quote

Also these are the ones that at least work with command line so they are the most promising.


Unless you lied in your previous posts, the one that worked in the shell was Mobile\ Documents, not Mobile/Documents. \ is how you escape spaces in the shell. You don't need to do that in Python, so it should just be "Mobile Documents". No slash, no backslash, no nothing. Just a space.

This post has been edited by sepp2k: 06 April 2018 - 03:50 PM

Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2