Total garbage... as well as way too long. Time to buy an extra drive. >>> x ="c:\somepath\aSubfolder\very_long\no_good\nix\this"
>>> print(x)
c:\somepath Subfolder ery_long
o_good
ix his
>>> print("{!r:}".format(x))
'c:\\somepath\x07Subfolder\x0bery_long\no_good\nix\this'
>>>
- 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 >>> x = r"c:\somepath\aSubfolder\very_long\no_good\nix\this"
>>> print(x)
c:\somepath\aSubfolder\very_long\no_good\nix\this
>>> print("{!r:}".format(x))
'c:\\somepath\\aSubfolder\\very_long\\no_good\\nix\\this'
>>>
- Use raw formatting, the little r in front goes a long way.
-------------------------------------------------------------------------------------------------------------------------- Solution 2... double backslashes >>> x ="c:\\somepath\\aSubfolder\\very_long\\no_good\\nix\\this"
>>> print(x)
c:\somepath\aSubfolder\very_long\no_good\nix\this
>>> print("{!r:}".format(x))
'c:\\somepath\\aSubfolder\\very_long\\no_good\\nix\\this'
>>>
- Yes! I cleverly used raw formatting and everything should be fine but notice the difference between str and repr.
-------------------------------------------------------------------------------------------------------------------------- Solution 3... forward slashes >>> x ="c:/somepath/aSubfolder/very_long/no_good/nix/this"
>>> print(x)
c:/somepath/aSubfolder/very_long/no_good/nix/this
>>> print("{!r:}".format(x))
'c:/somepath/aSubfolder/very_long/no_good/nix/this'
>>>
-------------------------------------------------------------------------------------------------------------------------- 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) >>> x = r"F:\Writing_Projects\Before_I_Forget\Scripts\timeit_examples.py"
>>> base_name = os.path.basename(x)
>>> dir_name = os.path.dirname(x)
>>> os.path.split(joined)
('F:\\Writing_Projects\\Before_I_Forget\\Scripts', 'timeit_examples.py')
>>> joined = os.path.join(dir_name,base_name)
>>> joined
'F:\\Writing_Projects\\Before_I_Forget\\Scripts\\timeit_examples.py'
>>>
>>> os.path.exists(joined)
True
>>> os.path.isdir(dir_name)
True
>>> os.path.isdir(joined)
False
>>>
ad nauseum -------------------------------------------------------------------------------------------------------------------------- Gotcha's Fixes often suggest the following ... what can go wrong, if you failed to check. (1) >>> x = "c:\somepath\aSubfolder\very_long\no_good\nix\this"
>>> new_folder = x.replace("\\","/")
>>> print(x)
c:\somepath Subfolder ery_long
o_good
ix his
>>> print("{!r:}".format(x))
'c:\\somepath\x07Subfolder\x0bery_long\no_good\nix\this'
>>>
(2) >>> x = r"c:\new_project\aSubfolder\"
File "<string>", line 1
x = r"c:\new_project\aSubfolder\"
^
SyntaxError: EOL while scanning string literal
(3) >>> x = "c:\new_project\New_Data"
>>> y = "new_grid"
>>> out = x + "\\" + y
>>> print(out)
c:
ew_project\New_Data\new_grid
(4) >>> x = r"c:\new_project\New_Data"
>>> z = "\new_grid"
>>> out = x + z
>>> print(out)
c:\new_project\New_Data
ew_grid
(5) This isn't going to happen again! >>> x = r"c:\new_project\New_Data"
>>> z = r"\new_grid"
>>> out = x + y
>>> print(out)
c:\new_project\New_Datanew_grid
(6) Last try >>> x = r"c:\new_project\New_Data"
>>> z = r"new_grid"
>>> please = x + "\\" + z
>>> print(please)
c:\new_project\New_Data\new_grid
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. |