Shell Script to Delete Selected Directory based on Changing date - python

My requirement is to delete the previous day directory and create a new directory in the format like as given in the below screen.
We generally make a directory in a below format taking into account the day and the date.
For example:
TP1_<TODAY_DAY>_<TODAY_DATE> TP1_TUE_19JUN2018.
How can this be achieved?

to get it into a variable on linux shell you can use the following:
export mydate=$(date +%a_%d%b%Y|tr [a-z] [A-Z])
then you can use the variable as part of cd, mkdir or any other command, i.e.
echo TP1__ TP1_$mydate
will give as result, please note that I used it on a Italian Cent OS Linux,
TP1__ TP1_MAR_19GIU2018

Related

How to change multiple files creation date using python script?

When i was importing my photos from my ipad to my hard disk i mistakenly imported them with the creation date of that day.
So now the true date for each photo is the modified date of it. I basically use this command to Setfile -d "$(GetFileInfo -m _FILE_PATH_)" _FILE_PATH_ to set the creation date of my photo to its modified date. But i was wondering if there is a way to put this command line in a python script where i can batch select multiple photos to preform this action.
Also, since if I open any photo the system will change its modified date, the only way that i can guess which date some photos belong to is by sorting them by name to see which date the photos before and after it belong to.
Any ideas on how I can write a script that deduces the real date of photos which have dates larger than a specific date based on the photos before and after it?
For example, you see in the screenshot that there is a photo from May 8 between two photos from October 5, so clearly it should be taken on October 5 as well.
But unfortunately sometimes the modified date of the photos before and after a photo are also wrong so I think the program has to look for the smallest date before and after the photo to deduce the real date.
UPDATE
I wrote this code on the python and it works fine for single files when I drag and drop them into the terminal to give the program the path. Im wondering if there is a way to do this with multiple files.
from subprocess import call
import os
path = input("enter filepath: ")
info = '"$(GetFileInfo -m '+ path + ')" '
command = 'Setfile -d ' + info + path
print('Setfile -d ' + info + path)
call(command, shell=True)
Not requiring interactive input is probably a crucial improvement so that you can run this on a large number of files at the same time. The way to do that in Python is with sys.argv.
Also, rather than subprocess.call, use subprocess.check_call (or subprocess.check_output to get the output) so that you can avoid invoking a shell, and check that the subprocess completed successfully. (There will be a traceback if not; if you want something more user-friendly, wrap it in try/except. Probably look for existing questions before posting another question asking for help with that.)
from subprocess import check_output, check_call
from sys import argv
for path in argv[1:]:
date = check_output(['GetFileInfo', '-m', path])
check_call(['Setfile', '-d', date, path])
Use it like
python3 script.py file1.png /path/to/file2.png ../elsewhere/file3.png ...
(Relative paths are resolved starting from your current working directory; no path resolves to the current directory.)

diff output of two python programs in windows cmd

So I am trying to compare output of two python programs, which have files that I will call trace1.py and trace2.py. Currently I am using process substitution with diff to try and compare their outputs, however I'm having trouble with finding both files, since they are in separate sub-directories of my current directory:
diff <(python /subdir1/tracing1.py) <(python /subdir2/tracing2.py)
When I run this, I get
The system cannot find the file specified.
I think I'm messing up some sort of path formatting, or else I'm using the process substitution incorrectly.
EDIT: In the end I decided that I didn't need to use process substitution, and instead could just diff program output after each program is run. However thanks to Fallenreaper in the comments, I was able to find a single command that does what I initially wanted:
python subdir1/tracing1.py > outfile1.txt & python subdir2/tracing2.py > outfile2.txt & diff outfile1.txt outfile2.txt
Sorry, not enough rep to comment yet :(
Your line works perfectly when you remove that slash. I would suggest using absolute path names or a relative path from current directory cos that front slash would take you to your root directory.
Cheers.

Python: Create a directory with data and time

By using subprocess module , how can we create a directory with today's date and time as directory name ?
I can follow one process , like assigning todays date to a variable in the the python and use that variable as reference to create a directory.
And I am using windows as my target machine.
but is there any other best ways I could follow ?
Thanks
If you think that you can rely upon your system's timezone setting, you may use built in date command (on Unix-like systems) in a way like this:
os.system("mkdir `date +%Y-%m-%d_%H:%M:%S`")
Though, there are other solutions, like to use os.mkdir().
Try it out.
The following will create a folder with the current date as its name. See the 'man date' to adjust the output to your liking.
import subprocess
p = subprocess.Popen('mkdir "$(date)"', shell=True)

Python relpath /home/<username> vs. ~

In Python3.4 (and also 2.6), using the os.path.relpath cmd, I get different answers if I run:
os.path.relpath("~/foo/bar.txt", "~")
than if I run
os.path.relpath("~/foo/bar.txt", "/home/<username>/")
(where I replace <username> with my user name, naturally!)
Namely, if I'm in my home directory, in the first case I get
foo/bar.txt
(as I would expect), but in the second case I get
~/foo/bar.txt
which is not what I would expect, given that ~ is exactly /home/<username>. (I get the same result in the second case regardless of whether I include the trailing '/' in the second arg.)
The second case gives an even more bizarre result if I run this within a subdirectory of my home directory, say ~/Data/:
Data/~/foo/bar.txt
which unless I'm mistaken is a non-existent directory.
I can figure out a work-around for this, but what is the logic for why it's happening this way? I don't suppose it's a bug, since it's been around a long time.
~ isn’t actually always your home directory—that’s only your shell’s interpretation. You can create a directory named ~ anywhere you want, just as you could create a directory named orange. Since ~ could be a plain old directory name, Python is treating it that way.
If you want Python to interpret ~ as meaning the home directory, as a shell would do, you need to tell Python you want to replace ~ with the path to the home directory using os.path.expanduser before passing your directory names to relpath.

How is current file location calculated in vim? (value of expand('%:p'))

While using the (excellent) Python autocompletion of YouCompleteMe (Jedi apparently) sometimes I'm suddenly unable to save because what vim thinks the path to my current file changes. When I open a file with vim bpython/curtsiesfrontend/repl.py, running
:echo expand('%:p')
gives me
/Users/tomb/Dropbox/code/bpython/bpython/curtsiesfrontend/repl.py
but after I use the completion, the same command gives
bpython/curtsiesfrontend/repl.py
and I can no longer save the file because that's not a path that exists. :pwd gives /Users/tomb/Dropbox/code/bpython before and after - so there must be some other part to the equation "what is the full directory path to this file."
I'm wondering what vim commands or concepts I should be looking at to identify the issue. There must be some concept of current directory of a file that is changing (however vim calculates the value of expand('%:p')) but I don't know what to call it. What is the concept of current directory that is changing?
The current file location is relative to Vim current path, which can be set with :cd or :lcd.
In order to identify where it is being changed you could use the 'verbose' option along the :redir command:
redir #a
set verbose=9
<execute the steps to reproduce the issue>
redir end
set verbose&
new
put a
Then search for 'chdir'. Increasing the value of 'verbose' displays more information, but it also make it harder to execute each step to reproduce the issue. After identifying a smaller test procedure you could repeat these steps using a higher value for 'verbose'.

Categories

Resources