Move files with python3 - python-3.x

So I just found out that python modules os, shutil are no longer available to clone from: https://pypi.python.org/
and after some research I did not find any replacement..
So the question is: how to move files from dir/fileX -> /dir2/fileX
AND / OR is there possibility to rename the folder if needed to 'move' all the files in it?
Using Linux / macOS
Thank you.
Tl;dr OS and SHUTIL are part of standard library in python 3x

shutil is the default library in python versions. So you can use shutil.move(src, dest) to recursively move folder from one repository to another.

Related

What's the best way to import large python modules into a ROS package? - import errors python ROS

I want to include a large python package: Yolov7-https://github.com/WongKinYiu/yolov7.git into my already existing ROS package. The large python package consists of many files which depend on each other. I want this to be included and available for use in my main program scripts/main.py, which runs as a ROS node.
File structure: https://imgur.com/a/THP5WvP
PROBLEM: Files in yolov7 importing other files in yolov7 need to have the pathname package.yolov7.file_name, while the whole package is actually written on the form: from file_name import ...
How can I make sure files in yolov7 actually can import other files in yolov7 without having to add the extra path to every import?
Example: instead of having to write: from package.yolov7.utils.datasets import letterbox
I want to be able to have: from utils.datasets import letterbox
I have tried editing CMakeLists.txt, setup.py, and package.xml, but for now they are unchanged in regards to yolov7.
I'm using ROS Noetic, running Ubuntu 5.4.123.

Where does python send files without a directory path?

I'm using Windows 10
Anyway I used os.rename and forgot to add a directory path to the beginning. Where did my files end up?
The file could be sent to the current working directory. To find the current working directory, use
import os
os.getcwd()
To find out where the file you are executing is located, use
import os
os.path.dirname(os.path.realpath(__file__))

location of Python libraries installed

I have installed Python 3.8 in my machine (windows 10). I can see it is installed in following locations, and my Python programs are running fine, after adding these locations in PATH variable.
C:\Users\<user name>\AppData\Roaming\Python\Python38
C:\Users\<user name>\AppData\Local\Programs\Python\Python38
My questions:
Why is it installed in a local directory as well as a roaming directory?
In which directory I can find the source code of the functions like list append(), list insert(). I have tried looking for them in the site-package directory, but it has a huge number of sub-directories, and unable to figure out where exactly to find it.
To answer two of your questions:
you can find the source code in:
C:\Users<User>\AppData\Local\Programs\Python\Python39-32\Lib>
you can run 'where' in WINDOWS terminal to find out which Python version installed. Also run these Python snippet to confirm:
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Users\\<User>\\AppData\\Local\\Programs\\Python\\Python39-32'

Import a python module from within a packed module

So I builded a python package localy:
cgi#cgires:~$ pip list | grep mads
madscgi 0.1.0
Its nice! Afterwards I can use it in Jupyter Notebook, in iPython Shell, in Python Shell and even in python scripts outside the modules code. So it works as expected 100% outside the modules code:
Thats nice, but next I want to import code from one builded module (inside the package) into another python file (inside the package). Lets name it import_test.py and try it out:
So it fails if it is getting executed in the directory, where the package is build from. And it looks like, that the python interpreter is taking the parent directory (with the same name like the module) and this is failing.
Is is possible to enforce the usage of the installed pip-package?
As #MisterMiyagi pointed out, the problem was, that there were an upper folder which had the same name as the module.
Here: mads_cons is the upper folder from import_test.py. Therefore, the upper folder is getting imported instead of the via pip installed module. Thats it.
The file you want to import should either be in the same folder or referred to with the absolute path of it.
If that doesn't suit you, you can call sys.path
import sys
sys.path
You can keep your file in any of the directories sys.path returns.
Smart would be, if you keep the file inside.
......../site-packages/

How to extract a zip file using python 3

How to extract a zip file using python when zip file present in different directory where script file present.
I try this ,but i got error because source path is not accepted ,try to solve me this problem.
from zipfile import ZipFile
def func(source, target):
with ZipFile('source', 'target'):
ZipFile.Extractall('target')
Use this code. To move through directories you could either hard code the directory where your script is present or you could toggle through the directories using simple commands such as "../" to move out of the given directory or "/" to move inside a folder in the directory. For example - "../script.py" or "/folder/script.py". Similarly you can use this to find your .zip file.
import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
zip_ref.extractall("targetdir")
For just unpacking, shutil should suffice:
import shutil
shutil.unpack_archive('path-to-zipfile')
You'll have to check for source path of the zip file which is relative to your current working directory. To know your current working directory you can try
import os
print(os.getcwd())
zip - Unzipping files in python
relative-paths-in-python

Resources