Downloading multiple .SAO files via http via Python3 - python

I need to use python3 to download multiple .SAO files from http. Each file has a distinct url which differs by a number in the url (for example, https://www.1.SAO, https://www.2.SAO, etc.). How do I achieve this, and how do I control where the files will be downloaded to?

Downloading a file is pretty simple using wget, and changing the URL can be done by modifying the string before executing the download() function.
to install wget, do pip install wget which will install wget to your default python instance. After that, just import it and you're good to go!
For example, if I wanted to download the .sao files from 1 to 10:
import wget
for i in range(1,11):
url = "https://www.{0}.SAO".format(i) #adds the value of "i" in at "{0}"
wget.download(url)
this will download all the files between https://www.1.SAO and https://www.10.SAO and save it to the working directory of the script.
If you wanted to change the directory, wget.download() has an optional second argument. For example, if I wanted to save my file to a directory called downloads which is located in the same directory as the script, I could call:
wget.download(url,"downloads/")
It would then save all my files in that subdirectory, instead of the working directory. If my directory is in an entire different part of the system (Let's say I wanted to download them to /usr/bin/ or something, I can specify that as well, the same way as using a normal Unix file path:
wget.download(url,"/usr/bin/")
Hopefully this helps you get started.

Related

How to change directory in my terminal using python

The problem I have is that when I am in my terminal and I am at the base. I want to for instance change my directory to downloads I was wondering how I would do that because then after I would change my directory I would be able to access certain files in download and tell the computer that I want all the information in this file so my code can run properly.
Thank you
You could try to use os;
os.system('cd ./Downloads')

Downloading zip folder/file from google drive from shared with me folder

I have been provided with access to a zip file/folder which is stored in my google drive and inside "Shared with me".
How can I download it to my laptop through terminal using "wget" or python or anything related.
The url for the whole folder within which it is contained goes like, https://drive.google.com/drive/folders/13cx4SBFLTX8CqIqjjec9-pcadGaJ0kNj
and the shareable link to the zip file is https://drive.google.com/open?id=1PMJEk3hT-_ziNhSPkU9BllLYASLzN7TL.
Since the files are 12GB in size in total, downloading them by clicking is quite tiresome when working with Jupyter notebook.
download the whole folder
!pip uninstall --yes gdown # After running this line, restart Colab runtime.
!pip install gdown -U --no-cache-dir
import gdown
url = r'https://drive.google.com/drive/folders/1sWD6urkwyZo8ZyZBJoJw40eKK0jDNEni'
gdown.download_folder(url)
You can check my answer here (Updated March 2018):
https://stackoverflow.com/a/49444877/4043524
There is one little issue with your case. In your situation, the format of the URL is different from the one mentioned in the link given above.
But, don't worry, you just have to copy the ID (a random-looking string in front of "id" key in the URL) and replace the FILEIDENTIFIER in the script with it.

Folders installed by Python Markdown

When I installed Python Markdown, I noticed that it added files to a build/docs/extensions folder. Where can I find this folder? I've searched through my machine, but came up empty.
As part of the build process, Python-Markdown also builds the docs (from Markdown text files into HTML files). However, as the generated files are written to the build directory, they are deleted in the cleanup step after install (the build dir is deleted). The docs are hosted here, but if you would like a local copy, you can build them yourself.
First you need a copy if the source files either from PyPI or GitHub. then from within the top directory, run the following command:
python setup.py build_docs
The docs will be written to build/docs. As we didn't complete the install process, the files haven't been deleted yet.
The complete steps to download and build might look this (on Linux using the current release of Python-Markdown):
wget http://pypi.python.org/packages/source/M/Markdown/Markdown-2.5.2.tar.gz
tar xvzf Markdown-2.5.2.tar.gz
cd markdown-2.5.2/
python setup.py build_docs
cd build/docs
From that point you can move/copy the files to wherever you would like them, or open them in your browser of choice to view them.

How to access files inside a Python egg file?

This might be a weird requirement but it's what I've run into. I Googled but yield nothing.
I'm coding an application who's using a lot of constant attributes / values recorded in an XML file (they'll not change so a static file), things work fine until I generated an egg file for it.
When the logic reaches the XML accessing part, I got one complaint like this:
/home/Workspace/my_proj/dist/mps-1.2.0_M2-py2.6.egg/mps/par/client/syntax/syntax.xml
Actually I've bundled the XML file in the path above but seems Python doesn't know how to access it.
The code to access the XML is as...
file_handler = open(path_to_the_file)
lines = file_handler.read().splitlines()
Any idea?
egg files are zipfiles, so you must access "stuff" inside them with the zipfile module of the Python standard libraries, not with the built-in open function!
If you want to access the contents inside the .egg file you can simply rename it and change extension from .egg to .zip and than unzip it.
Which will create a folder and the contents will be same as they were when it was a .egg file
for example brewer2mpl-1.4.1-py3.6.egg
After Renaming brewer2mpl-1.4.1-py3.6.zip
Now if we open it, it'll get easily unzipped and the content will be put in a folder with same name in the same directory.
(tested on macOS Sierra)
The less command on *nix systems can peek inside zip files. Therefore less some.egg will list the contents of a .egg file too.
Just run unzip file.egg
You can install unzip on Debian/Ubuntu with
sudo apt install unzip
or on macOS by installing Homebrew then
brew install unzip
Access file from inside egg file
Yes, It is possible to read the files from inside egg file.
Egg file: mps-1.2.0_M2-py2.6.egg structure for module level example:
In driverfile.py:
import xml.etree.ElementTree
import mps.par.client as syntaxpath
import os
path = os.path.dirname(syntaxpath.__file__)
element = xml.etree.ElementTree.parse(path+'\\syntax\\syntax.xml').getroot()
print(element)
Read xml file from inside an eggfile:
PYTHONPATH=mps-1.2.0_M2-py2.6.egg python driverfile.py
i think by default eggs packing file under python won't add your xml inside to the pack

How to convert MP3 to WAV in Python

If I have an MP3 file how can I convert it to a WAV file? (preferably, using a pure python approach)
I maintain an open source library, pydub, which can help you out with that.
from pydub import AudioSegment
sound = AudioSegment.from_mp3("/path/to/file.mp3")
sound.export("/output/path/file.wav", format="wav")
One caveat: it uses ffmpeg to handle audio format conversions (except for wav files, which python handles natively).
note: you probably shouldn't do this conversion on GAE :/ even if it did support ffmpeg. EC2 would be a good match for the job though
This is working for me:
import subprocess
subprocess.call(['ffmpeg', '-i', 'audio.mp3',
'audio.wav'])
I think I am right person to answer this question because I am student who tried hard to get answer for this question. I am giving answer for Windows users but I think this may work with MAC OS too. But apt for windows.
Lets discuss answers in steps:
first check for pydub and ffmpeg package. If you computer dont have these packages then install pydub in you command prompt
pip install pydub
Next and imp thing is ffmpeg package which converts images to different formats. For this you should manually install this package. Let me give you reason why when we can use pip for installing package. First pip installs the package but it will not stores the path to the system. So computer cant recognize this package path. For this I suggest you to install manually but how.... dont worry will give you steps.
STEP 1:
#Present link
This first link that you have paste it in google
https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip
#Use for future students
But people will have question now this link might work what about future. For that simple answer is
https://www.gyan.dev/ffmpeg/builds/
After typing this in google go to releases and download zip folder always don't download 7.zip.But thus is only when my first link will not work for future is any student search for answer.
STEP 2: After downloading the zip file from first step first link. Now make a folder in C drive. For this just click on my My PC, then OS(C:),make a new folder. Copy paste the zip file downloaded to this folder. Extract the zip file in this new folder. Now go into the folder and copy path of "bin" present in this folder from properties.
STEP 3:This is final step and imp one where you will set path. In search bar in your laptop search for "Edit the system environmental variables". Then click on "environmental variables" at bottom for path. Here they are two parts in screen system variables and user variables. Now you have to search for path "Path" in system variable is you want to use for whole system. Double click on "Path" in system variables. A window appears where you have to choose "New". Here copy paste the path of bin folder. Then click on Ok in all and close all tabs.
Step 4:Check for correct installation of ffmpeg. In command prompt type ffmpeg now you will get the list of paths and its features. This shows you have finished your installation.
Step 5 : Download a mp3 file. If your have downloaded python then open IDLE prompt. The click on new in File a note pad appears. One imp point to remember here is copy paste the mp3 file where you python code is stored. Example If I want to save the python file in Desktop the mp3 file should be stored in desktop. I think you go an idea. Now copy paste the code which I am using
import subprocess
subprocess.call(['ffmpeg', '-i', 'ind.mp3','ind1.wav'])
then click on run module
you will get the conversion.
Thank you
This answer might help you. If you want code and method for converting speech to text code and method you can post me. I wish this answer for 10 min may save you hours.
https://www.youtube.com/watch?v=vBb_eYThfRQ
use this video for path configuration or step 3 for reference but copy path to system variables not user because whole system can use this package then. If my language is bad don't mind I think it is understandable.
Install the module pydub. This is an audio manipulation module for Python. This module can open many multimedia audio and video formats. You can install this module with pip.
pip install pydub
If you have not installed ffmpeg yet, install it. You can use your package manager to do that.
For Ubuntu / Debian Linux:
apt-get install ffmpeg
When ready, execute the below code:
from os import path
from pydub import AudioSegment
# files
src = "transcript.mp3"
dst = "test.wav"
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
Check this link for details.
For Those using windows 7 and above:
Step 1: This link will help you install ffmpeg:
How to Install FFMPEG on Windows
Step 2: This code will help you convert multiple files from one format to another ( which of course is supported by ffmpeg)
import os
import subprocess
input_dir = r'C:\\Path\\To\\Your\\Input\\Directory\\'
output_dir = r'C:\\Path\\To\\Your\\Output\\Directory\\'
path_to_ffmpeg_exe = r'C:\\Path\\To\\ffmpeg-2022-YY-MM-git-blabla-full_build\\bin\\ffmpeg.exe'
files_list = []
for path in os.listdir(input_dir):
if os.path.isfile(os.path.join(input_dir, path)):
files_list.append(path)
for file_nm in files_list:
print(file_nm)
subprocess.call([path_to_ffmpeg_exe, '-i', os.path.join(input_dir, file_nm), os.path.join(output_dir, str(file_nm.split(".")[0] + ".wav"))])

Categories

Resources