Skip to content Skip to sidebar Skip to footer

How Do I Modify A Filepath Using The Os.path Module?

My code import os.path #gets the module beginning = input('Enter the file name/path you would like to upperify: ') inFile = open(beginning, 'r') contents = inFile.read() modde

Solution 1:

Change

final_name = os.path.join(head + new_new_name)

to

final_name = head + os.sep + new_new_name

Solution 2:

head from os.path.split doesn't have a trailing slash in the end. When you join the head and new_new_name by concatenating them

head + new_new_name 

you don't add that missing slash, so the whole path becomes invalid:

>>> head, tail = os.path.split('/etc/shadow')>>> head
'/etc'
>>> tail
'shadow'
>>> head + tail
'/etcshadow'

The solution is to use os.path.join properly:

final_name = os.path.join(head, new_new_name)

Post a Comment for "How Do I Modify A Filepath Using The Os.path Module?"