NULL.Dude
NULL.Dude

Reputation: 219

Error writing to folder in python os.makedirs()

I am trying to download a .zip file from ftp site, (works independent of the error), I am creating a folder in a directory with the current date in the name. I want the downloaded zip file to be placed in the newly created folder. my code is below.

import os
import urllib
import datetime

now = datetime.datetime.now()
situs = "ftp://pbcgis:[email protected]/CWGIS/SITUS_PUB.zip"
path = os.path.join(r"Y:\JH_Data_Dump\SITUS\PBC_SITUS" + str(now.month) + "_" + str(now.day) + "_" + str(now.year))
path1 = os.path.join(path + "PBC_SITUS" + str(now.month) + "_" + str(now.day) + "_" + str(now.year) +".zip")
print "Creating new directory..."
os.makedirs(path)
print "beginning PBC SITUS Download..."
urllib.urlretrieve(situs, path1)

I get no errors and the file downloads successfully but its not placing the .zip into my newly created folder, its placing it the same directory as the folder but not inside.

Upvotes: 0

Views: 539

Answers (2)

menesis
menesis

Reputation: 451

You use os.path.join incorrectly. Path segments - directories and filename - are distinct arguments. They are joined using path separator, either \ or /.

path = os.path.join('Y:', "PBC_SITUS123")
path1 = os.path.join(path, "PBC_SITUS123" + ".zip")

will result in Y:\PBC_SITUS123\PBC_SITUS123.zip

Upvotes: 2

NULL.Dude
NULL.Dude

Reputation: 219

I figured out why, I was missing a "\" in the path1 string

it should read:

path1 = os.path.join(path + r"\PBC_SITUS" + str(now.month) + "_" + str(now.day) + "_" + str(now.year) +".zip")

Upvotes: 0

Related Questions