Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:45:58+00:00 2026-05-22T14:45:58+00:00

I have this python script #!/usr/bin/env python import datetime, os from time import gmtime,

  • 0

I have this python script

#!/usr/bin/env python
import datetime, os
from time import gmtime, strftime

to_backup = "/home/vmware/tobackup"
var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
for f in os.listdir(to_backup):
    if(os.path.isfile(f)):
        print f + " is a file"
    if(os.path.isdir(f)):
        print f + " is a directory"

It is giving me empty ouput. i don’t know where is the problem

OUTPUT FOR dr jimbob answer

total 36
-rwxrwxr-x 1 vmware vmware  440 May  5 07:41 back.py
-rwxrwxr-x 1 vmware vmware 2624 May  4 20:35 backup.sh
drwxr-xr-x 2 vmware vmware 4096 Jun 22  2010 Desktop
drwxrwxr-x 2 vmware vmware 4096 May  5 03:51 destination
drwxr-xr-x 2 root   root   4096 May  4 18:49 public_html
drwxrwxr-x 2 vmware vmware 4096 May  1 07:47 python
-rwxrwxr-x 1 vmware vmware  560 May  1 13:20 regex.py
drwxrwxrwx 7 vmware vmware 4096 May  5 03:50 tobackup
total 20
drwxrwxrwx 2 vmware vmware 4096 May  5 03:50 five
drwxrwxrwx 2 vmware vmware 4096 May  5 03:50 four
drwxrwxrwx 2 vmware vmware 4096 May  5 03:50 one
drwxrwxrwx 2 vmware vmware 4096 May  5 03:50 three
drwxrwxrwx 2 vmware vmware 4096 May  5 03:50 two
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:45:59+00:00Added an answer on May 22, 2026 at 2:45 pm

    Ok you have permission, but you aren’t in the right directory when you list through the files. list_dir gives you a list of dirs/files without their path, and os.path.isfile('one') and os.path.isdir('one') will check whether the directory ‘one’ exists in the current directory (wherever you launched the script from, unless you explicitly changed directory with os.chdir or included the path, e.g., os.path.isdir('/home/vmware/tobackup/one').

    #!/usr/bin/env python
    import datetime, os
    from time import gmtime, strftime
    import subprocess
    
    to_backup = "/home/vmware/tobackup"
    var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
    
    os.chdir(to_backup)
    # os.listdir(to_backup) = ['one', 'two', 'three', 'four', 'five']
    for f in os.listdir(to_backup):
        if(os.path.isfile(f)):
            print f + " is a file"
        if(os.path.isdir(f)):
            print f + " is a directory"
    

    or

    to_backup = "/home/vmware/tobackup"
    var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
    # os.listdir(to_backup) = ['one', 'two', 'three', 'four', 'five']
    for f in os.listdir(to_backup):
        if(os.path.isfile(os.path.join(to_backup,f))):
            print f + " is a file"
        if(os.path.isdir(os.path.join(to_backup,f))):
            print f + " is a directory"
    

    or with walk (but not actually walking through subdirs).

    to_backup = "/home/vmware/tobackup"
    var1 = datetime.datetime.now().strftime('%b-%d-%I%p')
    
    root, dirs, files in os.walk(to_backup).next()
    for file in files:
        print f + " is a file in " + root
    for dir in dirs:
        print f + " is a directory"
    

    EDIT: To be even clearer, the mistake with your original script is you have a file structure like:

    /home/user/bin/your_script.py
    /home/vmware/tobackup/
    /home/vmware/tobackup/one
    /home/vmware/tobackup/two
    ...
    

    When you go to /home/user/bin to run your script (e.g., python your_script.py), os.listdir('/home/vmware/tobackup') gives you a list of file and dir names in /home/vmware/tobackup, that is ['one','two', ...]. However, when you do os.path.isfile('one') from the directory /home/user/bin, you check to see if /home/user/bin/one is a file, not whether /home/vmware/tobackup/one is a file. Since /home/user/bin/one doesn’t exist, you get no output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've set up this simple script on my local machine: #!/usr/bin/python import socket from
I have a script that looks something like this: #!/usr/bin/env python # encoding: utf-8
I have a scripts (a.py) reads in 2 parameters like this:- #!/usr/bin/env python import
Consider the code below: #!/usr/bin/env python from PyQt4 import QtCore, QtGui import os,sys class
Let's say I have a basic Python script, test.py : #!/usr/bin/python print Content-type: text/html\n\n
I have a python script the runs this code: strpath = sudo svnadmin create
I have a Python script that is running a few ls commands. This script
I have been learning python for some time now. While starting this learning python
This python script is the best I have come up with so far. I
I have this code in Python inputted = input(Enter in something: ) print(Input is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.