I am trying to create directory in Ubuntu using python and save my zip files in it. My code is working fine in windows but behaving weirdly with ubuntu.
import os
import zipfile
import datetime
from os.path import expanduser
home = expanduser('~')
zip_folder = home + '\\Documents\\ziprep' # enter folder path where files are
zip_path = home + '\\Documents\\zips' #enter path for zip to be saved
global fantasy_zip
def dailyfiles(weekly_file,today):
today = str(today)
try:
os.mkdir(zip_path + today)
except OSError:
print("Creation of the directory %s failed" % today)
else:
print("Successfully created the directory %s " % today)
for folder, subfolders, files in os.walk(zip_folder):
for file in files:
if file.startswith(today) and not file.endswith('.zip') and file not in weekly_file:
print("Zipping - Filename " + file)
zip_in = zip_path + today + "\\"
fantasy_zip = zipfile.ZipFile(zip_in + file + '.zip', 'w')
fantasy_zip.write(os.path.join(folder, file),
os.path.relpath(os.path.join(folder, file), zip_folder),
compress_type=zipfile.ZIP_DEFLATED)
fantasy_zip.close()
def main():
weekday = str(datetime.datetime.today().weekday())
today = datetime.date.today().strftime('%Y%m%d')
dailyfiles(weekly_file,today)
if __name__ == '__main__':
main()
Logically it should create a folder with todays date at the path specified. But it is creating Folder in ubuntu with the whole path at the same directory where m script is.
For example it is creating folder with name like this: '/home/downloads/scripypath'
Whereas I need '20191106' at the path which is specified in script. The code is working fine in windows.
Link to current project file
2 Answers 2
in ubuntu directory structure is totally different and they use \ instead of /. so prepare your link as ubuntu file structure.
Comments
I suggest using home + '/Documents/ziprep/'
and home + '/Documents/zips/'
on lines 8 and 9, respectively.
EDIT: Sorry, forgot why this should solve the problem. In Linux or Unix, directories use "/" instead of "\" (used in Windows) as directory separators.
/
then `\\` when specifying path.\\foo\\bar
to create directories in Linux, that sould be'foo/bar/'
.