1

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

asked Jun 11, 2019 at 16:14
5
  • Please truncate the code to only the relevant section. Commented Jun 11, 2019 at 16:17
  • 1
    it's better to use / then `\\` when specifying path. Commented Jun 11, 2019 at 16:22
  • 1
    Because directories paths in windows and Linux are not the same, you can't use \\foo\\bar to create directories in Linux, that sould be 'foo/bar/'. Commented Jun 11, 2019 at 16:22
  • @rdas truncate the code. Commented Jun 11, 2019 at 16:23
  • @c0x6a its working for me. Now I am getting a different issue. But its this one is resolved. Just one question. I Linux I get an error Variable referenced before assignment. But not in windows. Why such behavior difference Commented Jun 11, 2019 at 16:36

2 Answers 2

2

in ubuntu directory structure is totally different and they use \ instead of /. so prepare your link as ubuntu file structure.

answered Jun 11, 2019 at 16:45

Comments

2

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.

answered Jun 11, 2019 at 16:41

3 Comments

Could you provide some information as to how this solves @Duck_dragon's question?
@PeonProgrammer Edited it just now. Thanks for the reminder!
Hi @Jainil Patel! I do believe that I did not copy any answer from this thread. My answer is authentic. In regards to you accusing me of copying your answer, I would never copy an answer that is completely wrong! Your answer states that Linux directories use '\' instead of '/'. According to official Ubuntu documentation (help.ubuntu.com/community/LinuxFilesystemTreeOverview), it is stated that Linux use '/', which was my answer. Instead of wrongly accusing me of copying answers, you might as well check your answer first before posting it. Please think before you click.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.