Backup script doesn't walk the files in a folder












0















I'm new to Python and one of my first projects is making a backup script.



The script creates a zip file with the name of the folder and a numerical number that will go up every time it's executed.



But it seems that the script only creates the zip file, but it doesn't add any files.



import zipfile, os
def backupToZip(folder):
# Backup the entire contents of "folder" into a ZIP file.
folder = os.path.abspath(folder) # make sure folder is absolute
# Figure out the filename this code should use based on
# what files already exist.
number = 1
while True:
zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
if not os.path.exists(zipFilename):
break
number = number + 1
# TODO: Create the ZIP file.
print('Creating %s...' % (zipFilename))
backupZip = zipfile.ZipFile(zipFilename, 'w')
# TODO: Walk the entire folder tree and compress the files in each folder.
for foldername, subfolders, filenames in os.walk(folder):
print('Adding files in %s...' % (foldername))
# Add the current folder to the ZIP file.
backupZip.write(foldername)
# Add all the files in this folder to the ZIP file.
for filename in filenames:
newBase / os.path.basename(folder) + '_'
if filename.startswith(newBase) and filename.endswith('.zip'):
continue # don't backup the backup ZIP files
backupZip.write(os.path.join(foldername, filename))
backupZip.close()
print('Done.')

backupToZip('C:\Usersxe76572\Downloads\carpetaWindows')


I'm trying to backup a folder with the following structure:
carpetaWindows.




  • text1.txt

  • text2.txt

  • text3.txt










share|improve this question

























  • This line doesn't look right: newBase / os.path.basename(folder) + '_' Are you sure that you copied your code correctly? That code should raise NameError on newBase.

    – PM 2Ring
    Nov 23 '18 at 14:12











  • The folder name you're backing up doesn't look right - is it really supposed to contain a hexadecimal escape sequence? Something like `Users?6572Downloads`?

    – jasonharper
    Nov 23 '18 at 14:36
















0















I'm new to Python and one of my first projects is making a backup script.



The script creates a zip file with the name of the folder and a numerical number that will go up every time it's executed.



But it seems that the script only creates the zip file, but it doesn't add any files.



import zipfile, os
def backupToZip(folder):
# Backup the entire contents of "folder" into a ZIP file.
folder = os.path.abspath(folder) # make sure folder is absolute
# Figure out the filename this code should use based on
# what files already exist.
number = 1
while True:
zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
if not os.path.exists(zipFilename):
break
number = number + 1
# TODO: Create the ZIP file.
print('Creating %s...' % (zipFilename))
backupZip = zipfile.ZipFile(zipFilename, 'w')
# TODO: Walk the entire folder tree and compress the files in each folder.
for foldername, subfolders, filenames in os.walk(folder):
print('Adding files in %s...' % (foldername))
# Add the current folder to the ZIP file.
backupZip.write(foldername)
# Add all the files in this folder to the ZIP file.
for filename in filenames:
newBase / os.path.basename(folder) + '_'
if filename.startswith(newBase) and filename.endswith('.zip'):
continue # don't backup the backup ZIP files
backupZip.write(os.path.join(foldername, filename))
backupZip.close()
print('Done.')

backupToZip('C:\Usersxe76572\Downloads\carpetaWindows')


I'm trying to backup a folder with the following structure:
carpetaWindows.




  • text1.txt

  • text2.txt

  • text3.txt










share|improve this question

























  • This line doesn't look right: newBase / os.path.basename(folder) + '_' Are you sure that you copied your code correctly? That code should raise NameError on newBase.

    – PM 2Ring
    Nov 23 '18 at 14:12











  • The folder name you're backing up doesn't look right - is it really supposed to contain a hexadecimal escape sequence? Something like `Users?6572Downloads`?

    – jasonharper
    Nov 23 '18 at 14:36














0












0








0








I'm new to Python and one of my first projects is making a backup script.



The script creates a zip file with the name of the folder and a numerical number that will go up every time it's executed.



But it seems that the script only creates the zip file, but it doesn't add any files.



import zipfile, os
def backupToZip(folder):
# Backup the entire contents of "folder" into a ZIP file.
folder = os.path.abspath(folder) # make sure folder is absolute
# Figure out the filename this code should use based on
# what files already exist.
number = 1
while True:
zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
if not os.path.exists(zipFilename):
break
number = number + 1
# TODO: Create the ZIP file.
print('Creating %s...' % (zipFilename))
backupZip = zipfile.ZipFile(zipFilename, 'w')
# TODO: Walk the entire folder tree and compress the files in each folder.
for foldername, subfolders, filenames in os.walk(folder):
print('Adding files in %s...' % (foldername))
# Add the current folder to the ZIP file.
backupZip.write(foldername)
# Add all the files in this folder to the ZIP file.
for filename in filenames:
newBase / os.path.basename(folder) + '_'
if filename.startswith(newBase) and filename.endswith('.zip'):
continue # don't backup the backup ZIP files
backupZip.write(os.path.join(foldername, filename))
backupZip.close()
print('Done.')

backupToZip('C:\Usersxe76572\Downloads\carpetaWindows')


I'm trying to backup a folder with the following structure:
carpetaWindows.




  • text1.txt

  • text2.txt

  • text3.txt










share|improve this question
















I'm new to Python and one of my first projects is making a backup script.



The script creates a zip file with the name of the folder and a numerical number that will go up every time it's executed.



But it seems that the script only creates the zip file, but it doesn't add any files.



import zipfile, os
def backupToZip(folder):
# Backup the entire contents of "folder" into a ZIP file.
folder = os.path.abspath(folder) # make sure folder is absolute
# Figure out the filename this code should use based on
# what files already exist.
number = 1
while True:
zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
if not os.path.exists(zipFilename):
break
number = number + 1
# TODO: Create the ZIP file.
print('Creating %s...' % (zipFilename))
backupZip = zipfile.ZipFile(zipFilename, 'w')
# TODO: Walk the entire folder tree and compress the files in each folder.
for foldername, subfolders, filenames in os.walk(folder):
print('Adding files in %s...' % (foldername))
# Add the current folder to the ZIP file.
backupZip.write(foldername)
# Add all the files in this folder to the ZIP file.
for filename in filenames:
newBase / os.path.basename(folder) + '_'
if filename.startswith(newBase) and filename.endswith('.zip'):
continue # don't backup the backup ZIP files
backupZip.write(os.path.join(foldername, filename))
backupZip.close()
print('Done.')

backupToZip('C:\Usersxe76572\Downloads\carpetaWindows')


I'm trying to backup a folder with the following structure:
carpetaWindows.




  • text1.txt

  • text2.txt

  • text3.txt







python zipfile






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:22









Mr Singh

1,116823




1,116823










asked Nov 23 '18 at 14:06









Green_SamGreen_Sam

75117




75117













  • This line doesn't look right: newBase / os.path.basename(folder) + '_' Are you sure that you copied your code correctly? That code should raise NameError on newBase.

    – PM 2Ring
    Nov 23 '18 at 14:12











  • The folder name you're backing up doesn't look right - is it really supposed to contain a hexadecimal escape sequence? Something like `Users?6572Downloads`?

    – jasonharper
    Nov 23 '18 at 14:36



















  • This line doesn't look right: newBase / os.path.basename(folder) + '_' Are you sure that you copied your code correctly? That code should raise NameError on newBase.

    – PM 2Ring
    Nov 23 '18 at 14:12











  • The folder name you're backing up doesn't look right - is it really supposed to contain a hexadecimal escape sequence? Something like `Users?6572Downloads`?

    – jasonharper
    Nov 23 '18 at 14:36

















This line doesn't look right: newBase / os.path.basename(folder) + '_' Are you sure that you copied your code correctly? That code should raise NameError on newBase.

– PM 2Ring
Nov 23 '18 at 14:12





This line doesn't look right: newBase / os.path.basename(folder) + '_' Are you sure that you copied your code correctly? That code should raise NameError on newBase.

– PM 2Ring
Nov 23 '18 at 14:12













The folder name you're backing up doesn't look right - is it really supposed to contain a hexadecimal escape sequence? Something like `Users?6572Downloads`?

– jasonharper
Nov 23 '18 at 14:36





The folder name you're backing up doesn't look right - is it really supposed to contain a hexadecimal escape sequence? Something like `Users?6572Downloads`?

– jasonharper
Nov 23 '18 at 14:36












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448173%2fbackup-script-doesnt-walk-the-files-in-a-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448173%2fbackup-script-doesnt-walk-the-files-in-a-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python