Python script not working in linux - python

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

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.

It works on my machine (ubuntu 10.10)
Maybe /home/vmware/tobackup is empty or you have no permissions to read it.

os.listdir(dir_name) returns filenames relative to the named directory. To use those in other commands, you need to either prepend the directory name (via f = os.path.join(to_backup, f) at the start of the loop body) or else change the working directory to the backup directory before starting the loop.
These are the first two alternatives shown in dr jimbob's answer.

Related

Raspberry Pi (Raspbian Linux flavor) run script on wifi up

Currently I am experience issues with the script automatic run after wifi adapter connects to a network.
After ridiculously extended research, I've made several attempts to add script to a /etc/network/if-up.d/. Manually my script works; however it does not automatically.
User permissions:
ls -al /etc/network/if-up.d/*
-rwxr-xr-x 1 root root 703 Jul 25 2011 /etc/network/if-up.d/000resolvconf
-rwxr-xr-x 1 root root 484 Apr 13 2015 /etc/network/if-up.d/avahi-daemon
-rwxr-xr-x 1 root root 4958 Apr 6 2015 /etc/network/if-up.d/mountnfs
-rwxr-xr-x 1 root root 945 Apr 14 2016 /etc/network/if-up.d/openssh-server
-rwxr-xr-x 1 root root 48 Apr 26 03:21 /etc/network/if-up.d/sendemail
-rwxr-xr-x 1 root root 1483 Jan 6 2013 /etc/network/if-up.d/upstart
lrwxrwxrwx 1 root root 32 Sep 17 2016 /etc/network/if-up.d/wpasupplicant -> ../../wpa_supplicant/ifupdown.sh
Also, I've tried to push the command directly in /etc/network/interfaces
by adding a row
post-up /home/pi/r/sendemail.sh
Contents of sendemail.sh:
#!/bin/sh
python /home/pi/r/pip.py
After the reboot, nothing actually happen. I've even tried sudo in front
I assume that wpasupplicant is the thing which causes that, but I cannot get how to run my script in ifupdown.sh script under /etc/wpa_supplicant.
Appreciate your help!
If you have no connectivity prior to initializing the wifi interface, I would suggest adding a cron job of a bash or python script that checks for connectivity every X minutes.
Ping (host);
If host is up then run python commands or external command.
This is rather ambiguous but hopefully is of some help.
Here is an example of a script that will check if a host is alive;
import re,commands
class CheckAlive:
def __init__(self):
myCommand = commands.getstatusoutput('ping ' + 'google.com)
searchString = r'ping: unknown host'
match = re.search(searchString,str(myCommand))
if match:
# host is not alive
print 'no alive, don't do stuff';
else:
# host is alive
print 'alive, time do stuff';

Sending files between computers remotely [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
How can I send and receive files remotely, and also push updates via python? We have a bunch of devices out in the market and they are all Windows ten based. How could we go about sending files to those machines and receive files from those machines? We would like to use python for this task. Any tutorials or articles could be awesome.
I wrote this script a while ago to send files to my remote SFTP server from my local laptop. The machines has each other's public keys:
import pysftp
import paramiko
fpaths = ['list/of', 'file/paths']
with pysftp.Connection(server, username='loginID') as sftp:
with sftp.cd('target/directory'):
for fpath in fpaths:
print("Sending:", fpath)
if not os.path.isdir(fpath):
sftp.put(fpath)
print("Permissioning", fpath)
sftp.chmod(os.path.basename(fpath), 755)
else:
dirname = os.path.basename(fpath)
if not sftp.isdir(dirname):
sftp.mkdir(dirname)
print("Permissioning", dirname)
sftp.chmod(os.path.basename(dirname), 755)
sftp.put_r(fpath, dirname)
sftp.walktree(dirname,
dcallback=lambda dname:print("Permissioning", dname) or sftp.chmod(dname, 755),
fcallback=lambda fname:print("Permissioning", fname) or sftp.chmod(fname, 755),
ucallback=lambda x:x)
Try using ftplib package for python ftp connection. Here is the small tutorial for that.
import ftplib
ftp = ftplib.FTP("www.python.org")
ftp.login("anonymous", "ftplib-example-1")
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
Executing above code example:
$ python ftplib-example-1.py
- total 34
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 .
- drwxrwxr-x 11 root 4127 512 Sep 14 14:18 ..
- drwxrwxr-x 2 root 4127 512 Sep 13 15:18 RCS
- lrwxrwxrwx 1 root bin 11 Jun 29 14:34 README -> welcome.msg
- drwxr-xr-x 3 root wheel 512 May 19 1998 bin
- drwxr-sr-x 3 root 1400 512 Jun 9 1997 dev
- drwxrwxr-- 2 root 4127 512 Feb 8 1998 dup
- drwxr-xr-x 3 root wheel 512 May 19 1998 etc
...
Else, you may go with the SSH using Paramiko. Use whichever suits you better.
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='none',
password='lol')
Ftplib code reference: The ftplib module
Paramiko code reference: SSH PROGRAMMING WITH PARAMIKO | COMPLETELY DIFFERENT

Python service - writing filename with timestamp

I wrote a Python script that will run indefinitely. It monitors a directory using PyInotify and uses the Multiprocessing module to run any new files created in those directories through an external script. That all works great.
The problem I am having is writing the output to a file. The filename I chose uses the current date (using datetime.now) and should, theoretically, roll on the hour, every hour.
now = datetime.now()
filename = "/data/db/meta/%s-%s-%s-%s.gz" % (now.year, now.month, now.day, now.hour)
with gzip.open(filename, 'ab') as f:
f.write(json.dumps(data) + "\n")
f.close() #Unsure if I need this, here for debug
Unfortunately, when the hour rolls on -- the output stops and never returns. No exceptions are thrown, it just stops working.
total 2.4M
drwxrwxr-x 2 root root 4.0K Sep 8 08:01 .
drwxrwxr-x 4 root root 12K Aug 29 16:04 ..
-rw-r--r-- 1 root root 446K Aug 29 16:59 2016-8-29-16.gz
-rw-r--r-- 1 root root 533K Aug 30 08:59 2016-8-30-8.gz
-rw-r--r-- 1 root root 38K Sep 7 10:59 2016-9-7-10.gz
-rw-r--r-- 1 root root 95K Sep 7 14:59 2016-9-7-14.gz
-rw-r--r-- 1 root root 292K Sep 7 15:59 2016-9-7-15.gz #Manually run
-rw-r--r-- 1 root root 834K Sep 8 08:59 2016-9-8-8.gz
Those files aren't really owned by root, just changed them for public consumption
As you can see, all of the files timestamps end at :59 and the next hour never happens.
Is there something that I should take into consideration when doing this? Is there something that I am missing running a Python script indefinitely?
After taking a peek. It seems as if PyInotify was my problem.
See here (https://unix.stackexchange.com/questions/164794/why-doesnt-inotifywatch-detect-changes-on-added-files)
I adjusted your code to change the file name each minute, which speeds up debugging quite a bit and yet still tests the hypothesis.
import datetime
import gzip, time
from os.path import expanduser
while True:
now = datetime.datetime.now()
filename = expanduser("~")+"/%s-%s-%s-%s-%s.gz" % (now.year, now.month, now.day, now.hour, now.minute)
with gzip.open(filename, 'a') as f:
f.write(str(now) + "\n")
f.write("Data Dump here" + "\n")
time.sleep(10)
This seems to run without an issue. Changing the time-zone of my pc was also picked up and dealt with. I would suspect, given the above, that your error may lie elsewhere and some judicious debug printing of values at key points is needed. Try using a more granular file name as above to speed up the debugging.

How can I get the Unix permission mask from a file? [duplicate]

This question already has answers here:
Checking File Permissions in Linux with Python
(5 answers)
Closed 2 years ago.
How can I get a file's permission mask like 644 or 755 on *nix using python?
Is there any function or class for doing that? Thank you very much!
os.stat is a wrapper around the stat(2) system call interface.
>>> import os
>>> from stat import *
>>> os.stat("test.txt") # returns 10-tupel, you really want the 0th element ...
posix.stat_result(st_mode=33188, st_ino=57197013, \
st_dev=234881026L, st_nlink=1, st_uid=501, st_gid=20, st_size=0, \
st_atime=1300354697, st_mtime=1300354697, st_ctime=1300354697)
>>> os.stat("test.txt")[ST_MODE] # this is an int, but we like octal ...
33188
>>> oct(os.stat("test.txt")[ST_MODE])
'0100644'
From here you'll recognize the typical octal permissions.
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
You are really only interested in the lower bits, so you could chop off the rest:
>>> oct(os.stat("test.txt")[ST_MODE])[-3:]
'644'
>>> # or better
>>> oct(os.stat("test.txt").st_mode & 0o777)
Sidenote: the upper parts determine the filetype, e.g.:
S_IFMT 0170000 bitmask for the file type bitfields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
I think this is the clearest way of getting a file's permission bits:
stat.S_IMODE(os.lstat("file").st_mode)
If the file is a symlink, os.lstat() will give you the mode of the link itself, whereas os.stat() dereferences the link. Therefore I find os.lstat() the most generally useful.
stat.S_IMODE() gets "the file’s permission bits, plus the sticky bit, set-group-id, and set-user-id bits".
Here's an example case, given regular file "testfile" and symlink to it, "testlink":
import stat
import os
print oct(stat.S_IMODE(os.lstat("testlink").st_mode))
print oct(stat.S_IMODE(os.stat("testlink").st_mode))
This script outputs the following for me:
0777
0666
Another way to do it if you don't want to work out what stat means is to use the os.access command http://docs.python.org/library/os.html#os.access
BUT read the docs about possible security issues
For instance to check permissions on the file test.dat which has read/write permissions
os.access("test.dat",os.R_OK)
>>> True
#Execute permissions
os.access("test.dat",os.X_OK)
>>> False
#And Combinations thereof
os.access("test.dat",os.R_OK or os.X_OK)
>>> True
os.access("test.dat",os.R_OK and os.X_OK)
>>> False
oct(os.stat('file').st_mode)[4:]
os.access(path, mode) method returns True if access is allowed on path, False if not.
available modes are :
os.F_OK - test the existence of path.
os.R_OK - test the readability of path.
os.W_OK - test the writability of path.
os.X_OK - test if path can be executed.
for example, checking file /tmp/test.sh has execute permission
ls -l /tmp/temp.sh
-rw-r--r-- 1 * * 0 Mar 2 12:05 /tmp/temp.sh
os.access('/tmp/temp.sh',os.X_OK)
False
after changing the file permission to +x
chmod +x /tmp/temp.sh
ls -l /tmp/temp.sh
-rwxr-xr-x 1 * * 0 Mar 2 12:05 /tmp/temp.sh
os.access('/tmp/temp.sh',os.X_OK)
True
Here is a simple way to check the permissions of a directory .
import os
import stat
mode = os.stat("path_of_directory").st_mode
if not ((mode & stat.S_IWUSR):
print('not writable by user')
if not ((mode & stat.S_IWUSR) and (mode & stat.S_IWGRP) and (mode & stat.S_IWOTH)):
print('not writable by all')
The flag list is herebelow :
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
There are a lot of file based functions inside the os module im sure. If you run os.stat(filename) you can always interprate the results.
http://docs.python.org/library/stat.html
os.stat is analogous to the c-lib stat (man 2 stat on linux to see the information)
stats = os.stat('file.txt')
print(stats.st_mode)
You can just run a Bash stat command with Popen if you want:
The normal Bash command:
jlc#server:~/NetBeansProjects/LineReverse$ stat -c '%A %a %n' revline.c
-rw-rw-r-- 664 revline.c
And then with Python:
>>> from subprocess import Popen, PIPE
>>> fname = 'revline.c'
>>> cmd = "stat -c '%A %a %n' " + fname
>>> out = Popen(cmd, shell=True, stdout=PIPE).communicate()[0].split()[1].decode()
>>> out
'664'
And here's another way if you feel like searching the directory:
>>> from os import popen
>>> cmd = "stat -c '%A %a %n' *"
>>> fname = 'revline.c'
>>> for i in popen(cmd):
... p, m, n = i.split()
... if n != fname:
... continue
... print(m)
break
...
664
>>>

How to get path of Start Menu's Programs directory?

...for current user? for all users?
I'm working an a small program which needs to create links in the start menu. Currently I'm hardcoding like below, but it only works in english locales, for example it should be "Startmenü" in german. What are cleaner, more portable approaches?
OUR_STARTMENU = os.environ['ALLUSERSPROFILE'] + '\Start Menu\Programs\Our Stuff'
thank you
I've heard of 2 ways of doing this. First:
from win32com.shell import shell
shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_COMMON_STARTMENU)
Second, using the WScript.Shell object (source : http://www.mail-archive.com/python-win32#python.org/msg00992.html):
import win32com.client
objShell = win32com.client.Dispatch("WScript.Shell")
allUserProgramsMenu = objShell.SpecialFolders("AllUsersPrograms")
userMenu = objShell.SpecialFolders("StartMenu")
Another source: http://blogs.msdn.com/saveenr/archive/2005/12/28/creating-a-start-menu-shortcut-with-powershell-and-python.aspx
Also, CSIDL_COMMON_STARTMENU is for all user startup and CSIDL_STARTMENU for current user startup.
A friend, Luke Pinner of Environment.gov.au, gave a solution by email which uses a core module (python 2.5+). Believed to be multi-lingual as the return from the API call is unicode. Tested on Win7 with Japanese locale, and on another us-english machine by manually changing Start Menu to point to %USERPROFILE%\Startmenü
''' Get windows special folders without pythonwin
Example:
import specialfolders
start_programs = specialfolders.get(specialfolders.PROGRAMS)
Code is public domain, do with it what you will.
Luke Pinner - Environment.gov.au, 2010 February 10
'''
#Imports use _syntax to mask them from autocomplete IDE's
import ctypes as _ctypes
from ctypes.wintypes import HWND as _HWND, HANDLE as _HANDLE,DWORD as _DWORD,LPCWSTR as _LPCWSTR,MAX_PATH as _MAX_PATH, create_unicode_buffer as _cub
_SHGetFolderPath = _ctypes.windll.shell32.SHGetFolderPathW
#public special folder constants
DESKTOP= 0
PROGRAMS= 2
MYDOCUMENTS= 5
FAVORITES= 6
STARTUP= 7
RECENT= 8
SENDTO= 9
STARTMENU= 11
MYMUSIC= 13
MYVIDEOS= 14
NETHOOD= 19
FONTS= 20
TEMPLATES= 21
ALLUSERSSTARTMENU= 22
ALLUSERSPROGRAMS= 23
ALLUSERSSTARTUP= 24
ALLUSERSDESKTOP= 25
APPLICATIONDATA= 26
PRINTHOOD= 27
LOCALSETTINGSAPPLICATIONDATA= 28
ALLUSERSFAVORITES= 31
LOCALSETTINGSTEMPORARYINTERNETFILES=32
COOKIES= 33
LOCALSETTINGSHISTORY= 34
ALLUSERSAPPLICATIONDATA= 35
def get(intFolder):
_SHGetFolderPath.argtypes = [_HWND, _ctypes.c_int, _HANDLE, _DWORD, _LPCWSTR]
auPathBuffer = _cub(_MAX_PATH)
exit_code=_SHGetFolderPath(0, intFolder, 0, 0, auPathBuffer)
return auPathBuffer.value

Categories