### Current file path and directory
current_file_path = os.path.realpath(__file__)
current_file_name = os.path.basename(current_file_path)
current_directory_path = os.path.dirname(current_file_path)
nearby_file_path = os.path.join(current_directory_path, nearby_file_name)
print current_file_path
print current_file_name
print current_directory_path
print nearby_file_path
print(os.getcwd().split(os.sep))
> current working directory path into a list of strings
path_parts = os.getcwd().split(os.sep)
print(os.sep.join(path_parts[:-2]))
> similar to ../..
### Current path of python interpreter (from inside python)
import sys
print sys.executable
help(sys)
- - -
def extract_valid_directory_path_and_name( path) :
if not path:
raise ValueError('did not receive a required PATH parameter')
if not path.startswith('/'):
raise ValueError('path must begin with a / , %s' % path)
parts = [ i for i in path.split('/') if len(i) > 0 ]
name = ''
if len(parts) == 0 : # checks the edge case that the string is all forward slashes
raise ValueError('Name is invalid: %s' % path)
elif len(parts) == 1:
name = parts.pop()
directoryPath = '/'
else:
name = parts.pop()
directoryPath = '/'.join(parts)
directoryPath = '/' + directoryPath
return directoryPath , name
# Main
print '%s %s' % (extract_valid_directory_path_and_name('/firstfolder'))
print '%s %s' % (extract_valid_directory_path_and_name('/firstfolder/secondsubfolder'))
print '%s %s' % (extract_valid_directory_path_and_name('/firstfolder/secondsubfolder/'))
print '%s %s' % (extract_valid_directory_path_and_name('/file.txt'))
print '%s %s' % (extract_valid_directory_path_and_name('/file.txt/'))
print '%s %s' % (extract_valid_directory_path_and_name('/extra//slashes///')) # not handled
print
try:
dir , name = extract_valid_directory_path_and_name(None)
except ValueError as e:
print e
try:
dir , name = extract_valid_directory_path_and_name('')
except ValueError as e:
print e
try:
dir , name = extract_valid_directory_path_and_name('/')
except ValueError as e:
print e
try:
dir , name = extract_valid_directory_path_and_name('//')
except ValueError as e:
print e
try:
dir , name = extract_valid_directory_path_and_name('missingrootslash')
except ValueError as e:
print e
# import os
# print os.path.posixpath(missingroot) # this converts to whatever os you are running on
import posixpath
print '\n\n%s (%s , %s) - empty string' % (rootpath, posixpath.dirname(posixpath.normpath(rootpath)) , posixpath.basename(posixpath.normpath(rootpath)))
print '%s (%s , %s)' % (rootfile, posixpath.dirname(posixpath.normpath(rootfile)) , posixpath.basename(posixpath.normpath(rootfile)))
print '%s (%s , %s)' % (rootfiletrailing, posixpath.dirname(posixpath.normpath(rootfiletrailing)) , posixpath.basename(posixpath.normpath(rootfiletrailing)))
print '%s (%s , %s)' % (extraslashes, posixpath.dirname(posixpath.normpath(extraslashes)) , posixpath.basename(posixpath.normpath(extraslashes)))
print '%s (%s , %s)' % (missingroot, posixpath.dirname(posixpath.normpath(missingroot)) , posixpath.basename(posixpath.normpath(missingroot)))
import posixpath
def __extract_directory_path_and_name(path):
""" if passed only the root then the name will be None
directory_path returned as empty string means the name is at the root
"""
normalized_path = posixpath.normpath(path)
directory_path = posixpath.dirname (normalized_path)
name = posixpath.basename(normalized_path)
return directory_path, name
print __extract_directory_path_and_name('/')
print __extract_directory_path_and_name('//')
print __extract_directory_path_and_name('///')
print __extract_directory_path_and_name('/folder')
print __extract_directory_path_and_name('/folder/')
print __extract_directory_path_and_name('file-missing-leading-slash.txt')
- - -
import os
import sys
projectPath = os.path.join(os.path.dirname(__file__) , os.pardir , 'AppEngine')
if not projectPath in sys.path:
sys.path.insert(0, projectPath) # ensure it is searched before other paths
from services.GatewayService import GatewayService
from models.Gateway import Gateway
# linux and URL forward slahes to windows
import os
import sys
import tempfile
local_root_path = os.path.join(tempfile.gettempdir())
print 'tempfile.gettempdir() is %s' % local_root_path
empty_path = ''
converted_path = os.path.normpath(empty_path) # becomes a .
print "%s becomes %s" % (repr(empty_path) , converted_path)
converted_path = os.path.normpath('/') # becomes a \
print '/ becomes %s' % (converted_path)
converted_path = os.path.normpath('/test/example/file.txt') # becomes a \test\example\file.txt
print '/test/example/file.txt becomes %s' % (converted_path)
converted_path = os.path.normpath('c:\\test\\file.txt') # becomes c:\test\file.txt
print 'c:\\test\\file.txt becomes %s' % (converted_path)
path = '/test/a/b/c'
#import pprint
from pprint import pprint
import Queue
q = Queue.Queue()
q.put('test')
q.put ('a')
#pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(q.queue)
pprint(q.queue)
print ''
if empty_path[0] == '/':
print 'never'
"""
if remote_folder_and_path.path == '':
converted_path = '' # do not convert blank into '.'
elif remote_folder_and_path.path[0] == '/':
stripped_path = remote_folder_and_path.path[1:] # strip any leading slashes
converted_path = os.path.normpath(stripped_path) # i.e. for downloading from my butt to Windows
else:
converted_path = os.path.normpath(remote_folder_and_path.path)
new_folder = os.path.join(converted_path , remote_folder_and_path.o2object.name) # name is a single string so does not need conversion
new_folder_full_local_path = os.path.join(self.local_destination_path , new_folder)
"""