Forgot path to module - python

I made a script called sm on a linux-os, which I want to update from python 3.6 to 3.9. But unfortunately I forgot the path to this file. I tried it with the command "locate sm.py" but it showed me thousands of things, where the name was something like the file name "sm.py", and I don't want to read through it. So does anyone know how to find this file more precisely than my locate-command?

try this in shell
find / -name "sm" -depth -exec echo {} \;

Related

Executing all files inside a folder in Python

I have 20 Python files which is stored inside a directory in ubuntu 14.04 like 1.py, 2.py, 3.py , 4.py soon
i have execute these files by "python 1.py", "python 2.py" soon for 20 times.
is their a way to execute all python files inside a folder by single command ?
find . -maxdepth 1 -name "*.py" -exec python3 {} \;
for F in $(/bin/ls *.py); do ./$F; done
You can use any bash construct directly from the command line, like this for loop. I also force /bin/ls to make sure to bypass any alias you might have set.
Use a loop inside the folder:
#!/bin/bash
for script in $(ls); do
python $script
done
You can try with the library glob.
First install the glob lybrary.
Then import it:
import glob
Then use a for loop to iterate through all files:
for fileName in glob.glob('*.py'):
#do something, for example var1 = filename
The * is used to open them all.
More information here: https://docs.python.org/2/library/glob.html

How can I use files/directories in python on Linux that start with .?

I'm a COMPLETE beginner to python, I'm making my first python script that really does anything. I'm assigning a directory to a variable so I can use it to move a file, but the directory includes a folder starting with . and python says it's invalid syntax. So how can I get python to ignore the .?
EDIT: Here's the code
#!/usr/bin/env python
import os
Optifine = find /home/Sol33t303/.minecraft -name
OptiFine_1.10.2_HD_U_E3.jar
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")
You are mixing two very different things.
Are you writing Python or Bash, cause this is totally Bash:
Optifine = find /home/Sol33t303/.minecraft -name
You can't just run Bash commands inside a Python script!
If, for example you want to run a shell command inside your script and get its output you can use:
Optifine = subprocess.check_output(['find', '/home/Sol33t303/.minecraft', '-name'])
And then you split the output by line, and foreach line (file found), move it to the desired destination:
for line in Optifine.splitlines():
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")

tensorflow-syntaxnet: Where are sentence_pb2 and gen_parser_ops?

I cannot import these two python files from syntaxnet and I cannot find them either. Does anyone know how to solve this issue? Thanks!
from syntaxnet.ops import gen_parser_ops
from syntaxnet import sentence_pb2
Those two are imported in syntaxnet/syntaxnet/conll2tree.py. You can get their locations by adding lines like below in conll2tree.py:
import os
print os.path.realpath(gen_parser_ops.__file__)
and then run the demo.sh as in the installation guide.
gen_parser_ops file is present under bazel-out/local-opt/genfiles/syntaxnet/ops/gen_parser_ops.py
sentence pb2 file is present in bazel-out/local-opt/genfiles/syntaxnet/sentence_pb2.py.
I found these files in my linux machine (ubuntu) using the find command:
sudo find ~/ -type f -name "gen_parser_ops*"
sudo find ~/ -type f -name "sentence_pb2*"

turn all files in a folder into function aliases with bash

I have a folder of python files. I want to turn them into functions I can call in bash. This following one-liner is in my profile text file loading at bash login, but it doesn't work. How can I fix it?
for i in `ls ~/Dropbox/Documents/tools/python`; do
fullfilename=$(basename "$i");
filename="${fullfilename%.*}";
$("alias $filename='/usr/local/bin/python3.3 ~/Dropbox/Documents/tools/python/$fullfilename '");
done
EDIT:
per Liviu Chircu's recommendation, I removed the " before and after the alias command. Following is now the new code:
for i in `ls ~/Dropbox/Documents/tools/python`; do
fullfilename=$(basename "$i");
filename="${fullfilename%.*}";
$(alias $filename='/usr/local/bin/python3.3 ~/Dropbox/Documents/tools/python/$fullfilename ');
done
Now I get this error:
$ findfactors 40
-bash: findfactors: command not found
Three steps to make the Python scripts executable from bash.
First, at the top of every file in that directory, add the following line to tell bash which interpreter to use.
#!/usr/local/bin/python3.3
Second, set all those files to executable.
chmod u+x ~/Dropbox/Documents/tools/python/*
Third, add that directory to your PATH. This one goes in ~/.bashrc, while the other two are one-time things.
export PATH=$PATH:$HOME/Dropbox/Documents/tools/python
Looks like you need to remove " from your alias command.
Try
$(alias $filename='/usr/local/bin/python3.3 ~/Dropbox/Documents/tools/python/$fullfilename ');
Instead of
$("alias $filename='/usr/local/bin/python3.3 ~/Dropbox/Documents/tools/python/$fullfilename '");
You mean something like
for i in "$HOME"/Dropbox/Documents/tools/python/*; do
fullfilename="$(basename "$i")"
filename="${fullfilename%.*}"
alias "$filename"="/usr/local/bin/python3.3 ~/Dropbox/Documents/tools/python/$fullfilename"
done

How to search for a list of file name on the server using linux bash shell tool

I am looking to see if the server has file name that i m looking for. I have list of file names on the text file and want to see whether these files existed in the server. Is there a way that I can do that either in python or in linux bash shell tool
Assuming the files holds the names as one per line, the following should work:
find / -type f | grep -f **filenames**
Please allow for a lengthy period of time to complete, if your machine is
slow or has lots of files.
If locate (updatedb) is available/up-and-running the following may be quicker.
locate -r . | grep -f **filenames**

Categories