Total garbage... as well as way too long. Time to buy an extra drive. <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN><SPAN class="string token">"c:\somepath\aSubfolder\very_long\no_good\nix\this"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># str notation</SPAN>
c<SPAN class="punctuation token">:</SPAN>\somepath Subfolder ery_long
o_good
ix his
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{!r:}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># repr notation</SPAN>
<SPAN class="string token">'c:\\somepath\x07Subfolder\x0bery_long\no_good\nix\this'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>- No r in front of the path.
- \a \b \n \t \v are all escape characters... check the result
- Notice the difference between plain str and repr notation
-------------------------------------------------------------------------------------------------------------------------- Solution 1... raw format <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"c:\somepath\aSubfolder\very_long\no_good\nix\this"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># str notation</SPAN>
c<SPAN class="punctuation token">:</SPAN>\somepath\aSubfolder\very_long\no_good\nix\this
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{!r:}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># repr notation</SPAN>
<SPAN class="string token">'c:\\somepath\\aSubfolder\\very_long\\no_good\\nix\\this'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>- Use raw formatting, the little r in front goes a long way.
-------------------------------------------------------------------------------------------------------------------------- Solution 2... double backslashes <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN><SPAN class="string token">"c:\\somepath\\aSubfolder\\very_long\\no_good\\nix\\this"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># str notation</SPAN>
c<SPAN class="punctuation token">:</SPAN>\somepath\aSubfolder\very_long\no_good\nix\this
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{!r:}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># repr notation</SPAN>
<SPAN class="string token">'c:\\somepath\\aSubfolder\\very_long\\no_good\\nix\\this'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>- Yes! I cleverly used raw formatting and everything should be fine but notice the difference between str and repr.
-------------------------------------------------------------------------------------------------------------------------- Solution 3... forward slashes <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN><SPAN class="string token">"c:/somepath/aSubfolder/very_long/no_good/nix/this"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># str notation</SPAN>
c<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">/</SPAN>somepath<SPAN class="operator token">/</SPAN>aSubfolder<SPAN class="operator token">/</SPAN>very_long<SPAN class="operator token">/</SPAN>no_good<SPAN class="operator token">/</SPAN>nix<SPAN class="operator token">/</SPAN>this
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{!r:}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># repr notation</SPAN>
<SPAN class="string token">'c:/somepath/aSubfolder/very_long/no_good/nix/this'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>-------------------------------------------------------------------------------------------------------------------------- Solution 4... os.path functions There are very useful functions and properties in os.path. The reader is recommended to examine the contents after importing the os module (ie dir(os.path) and help(os.path) <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"F:\Writing_Projects\Before_I_Forget\Scripts\timeit_examples.py"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> base_name <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>basename<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> dir_name <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>dirname<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN>joined<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># see splitdrive, splitext, splitunc</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'F:\\Writing_Projects\\Before_I_Forget\\Scripts'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'timeit_examples.py'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> joined <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>dir_name<SPAN class="punctuation token">,</SPAN>base_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> joined
<SPAN class="string token">'F:\\Writing_Projects\\Before_I_Forget\\Scripts\\timeit_examples.py'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>exists<SPAN class="punctuation token">(</SPAN>joined<SPAN class="punctuation token">)</SPAN>
<SPAN class="token boolean">True</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>isdir<SPAN class="punctuation token">(</SPAN>dir_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="token boolean">True</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>isdir<SPAN class="punctuation token">(</SPAN>joined<SPAN class="punctuation token">)</SPAN>
<SPAN class="token boolean">False</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> ad nauseum -------------------------------------------------------------------------------------------------------------------------- Gotcha's Fixes often suggest the following ... what can go wrong, if you failed to check. (1) <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> <SPAN class="string token">"c:\somepath\aSubfolder\very_long\no_good\nix\this"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> new_folder <SPAN class="operator token">=</SPAN> x<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"/"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># str notation</SPAN>
c<SPAN class="punctuation token">:</SPAN>\somepath Subfolder ery_long
o_good
ix his
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{!r:}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># repr notation</SPAN>
<SPAN class="string token">'c:\\somepath\x07Subfolder\x0bery_long\no_good\nix\this'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>(2) <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"c:\new_project\aSubfolder\"</SPAN>
File <SPAN class="string token">"<string>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN>
x <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"c:\new_project\aSubfolder\"</SPAN>
<SPAN class="operator token">^</SPAN>
SyntaxError<SPAN class="punctuation token">:</SPAN> EOL <SPAN class="keyword token">while</SPAN> scanning string literal
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>(3) <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> <SPAN class="string token">"c:\new_project\New_Data"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> y <SPAN class="operator token">=</SPAN> <SPAN class="string token">"new_grid"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> out <SPAN class="operator token">=</SPAN> x <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> y
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>out<SPAN class="punctuation token">)</SPAN>
c<SPAN class="punctuation token">:</SPAN>
ew_project\New_Data\new_grid
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> (4) <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"c:\new_project\New_Data"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> z <SPAN class="operator token">=</SPAN> <SPAN class="string token">"\new_grid"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> out <SPAN class="operator token">=</SPAN> x <SPAN class="operator token">+</SPAN> z
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>out<SPAN class="punctuation token">)</SPAN>
c<SPAN class="punctuation token">:</SPAN>\new_project\New_Data
ew_grid
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> (5) This isn't going to happen again! <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"c:\new_project\New_Data"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> z <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"\new_grid"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> out <SPAN class="operator token">=</SPAN> x <SPAN class="operator token">+</SPAN> y
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>out<SPAN class="punctuation token">)</SPAN>
c<SPAN class="punctuation token">:</SPAN>\new_project\New_Datanew_grid
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> (6) Last try <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"c:\new_project\New_Data"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> z <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"new_grid"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> please <SPAN class="operator token">=</SPAN> x <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> z
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>please<SPAN class="punctuation token">)</SPAN>
c<SPAN class="punctuation token">:</SPAN>\new_project\New_Data\new_grid
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> Well this isn't good! Lesson? Get it right the first time. Remember the next time someone says... Have you checked your file paths...????? Remember these examples. Curtis pointed out this helpful link...I will include it here as well Paths explained: Absolute, relative, UNC, and URL—Help | ArcGIS for Desktop That's all for now. I will deal with spaces in filenames in an update. I am not even to go to UNC paths. |