How do I execute this logcat command via python?
adb logcat -d > log1.txt
I tried this but there is no file being created in the output folder.
I did do a workaround and it worked.
I have another issue: The log1.txt file is populated. I am expected to copy lines with tag "sample" from log1.txt file to another new file. I wrote a function for this but only one instance of the tag line is getting printed in the new file where as the print statement before this print 3 lines in the output. Could someone plz help.
for line in open("log1.txt",'r'):
cmd="sample"
if cmd in line:
print line
f = open('myfile','w')
f.write(line)
Try this
with open('out-file.txt', 'w') as f:
subprocess.call(['adb','logcat', '-d'], stdout=f)
Related
my environment:
cpu:rk3288
os:android7.1
transfer method:sftp
I wrote android code to do these things below:
get the logcat with code "adb logcat -d -v time -f /mnt/sdcard/logcat.txt"
pull the file logcat.tx to the server with sftp
in 1st step I coding some java language with android studio like below, if anyone can help me, thanks!
Process p = Runtime.getRuntime().exec("adb logcat -d -v time -f /mnt/sdcard/logcat.txt");
error massage:
java.io.IOException: Cannot run program "adb": error=13, Permission denied
You can't use adb commands from inside the device, even if you somehow have it in the device you would need root permissions. The adb is the bridge between the PC and the device. Take a look here: https://developer.android.com/studio/command-line/adb
Although you probably can just remove the adb and use logcat directly, like:
Process p = Runtime.getRuntime().exec("logcat -d -v time -f /mnt/sdcard/logcat.txt");
Here you can see some more options about using the logcat command: Read logcat programmatically within application, including Reetpreet Brar's answer, that I think will be better for you:
Process pq=Runtime.getRuntime().exec("logcat v main"); //adapt the command for yourself
BufferedReader brq = new BufferedReader(new InputStreamReader(pq.getInputStream()));
String sq="";
while ((sq = brq.readLine()) != null)
{
//here you can do what you want with the log, like writing in a file to
//send to the server later.
}
Here you can choose methods to write your file: How to Read/Write String from a File in Android
Then, just send the file to the server.
I am trying to take a file and rename it based on text i have in a file. Basically i have an android APK that i want to rename to name_version.apk I am using AAPT to dump the version number of the apk into a version.txt file and so the output of cat version.txt looks like this
1.0b17
So how would I go about using either cat (or some other shell command) to rename MyApp.apk to MyApp_v1.0b17.apk ?
You can use backticks to capture the output from cat into a string, like this.
`cat version.txt`
From there, you can make a simple mv command with that in the arguments.
mv "MyApp.apk" "MyApp_`cat version.txt`.apk"
As I'm running an old Python version on android which gives incorrect file sizes for files > 4 GB I tried writing a workaround to get the correct sizes, code:
def getsize_workaround( filename ):
import subprocess as s
output = s.Popen("ls -l " + filename, shell=True, executable="/system/bin/sh", stdout=s.PIPE).communicate()[0]
size = long(re.split(r'\s+', output)[3])
return size
This works well when I try to call it using a simple python script:
print(getsize_workaround(path))
However, when I try to use it in my NZBGet VideoSort script it can't find ls and pops this error at: output = s.Popen("ls -l " + filename, shell=True, executable="/system/bin/sh", stdout=s.PIPE).communicate()[0]-> : /bin/sh: ls: not found. (function is called at line 824, see dropbox link below).
Haven't got a clue why it can't find ls anymore, anyone help is much appreciated. You can find the VideoSort script here: https://db.tt/oM3U5gZR.
PATH variable didn't include the correct directories when run from NZBGet. Fixed by setting os.environ['PATH'] manually. Thanks to abernert for the tip.
also try adding an alias in your bash script for the python version you want to use
alias python=<python version you want to use>
I'm working on a python script which takes the output from a previous terminal window command and inputs again. Here is the code
pathCmd = './adb shell pm path com.example.deliveryupdater'
pathData = os.popen(pathCmd,"r")
for line in pathData:
path = line
print line
if line.startswith("package:"):
apkPath = line[8:]
print apkPath
pullCmd = './adb pull ' + apkPath
pullData = os.popen(pullCmd,"r")
The output is as follows:
/data/app/com.example.deliveryupdater-1.apk
' does not exist/data/app/com.example.deliveryupdater-1.apk
It says the path doesn't exist.
When I hardcoded the path as
pullCmd = './adb pull /data/app/com.example.deliveryupdater-1.apk'
pullData = os.popen(pullCmd,"r")
The .apk data gets pulled.
3886 KB/s (2565508 bytes in 0.644s)
Is there a way I can pass as the string as a variable? Am I doing anything wrong here?
Please help
The error message is telling you what's wrong: that path, /data/app/com.example.deliveryupdater-1.apk(newline), does not exist. Probably there is not a filename ending with a newline in the directory. I assume you are iterating over lines from a file or something of that sort, which would explain why you have the newline. Why not just slice [8:-1] instead of [8:], or perhaps, just .rstrip() on the line (this will work even if the line doesn't have a newline, as the last line in the file might not)?
if line.startswith("package:"):
apkPath = line[8:].rstrip()
print apkPath
pullCmd = './adb pull ' + apkPath
pullData = os.popen(pullCmd,"r")
I am working in android application. I want to copy data from 1 file to other file.I have executed below command for it, but not able to copy data from file1 -> file 2.
// Executes the command.
String CAT_COMMAND = "cat /sdcard/file1 > /sdcard/file2";
Process process = Runtime.getRuntime().exec(CAT_COMMAND);
I have used ADB SHELL command at DOS , executed CAT command which works fine.
But code execution is not working into the real devices.
please help me out why does it is not copying data from file1->file2.??
provide resolution for writing/copying data using CAT command.
Thanks
can you try with the complete path /mnt/sdcard .(or) Try to get the right path by Environment.getExternalStorageDirectory()