Can't rename a directory that contains thousands of files - python-3.x

The directory I am trying to rename has around 5k parquet files.
Unfortunatelly, os and shutil libraries aren't helping with it
import os, shutil
os.rename('/dbfs/FileStore/AllInOneParquets/SdId=791221', '/dbfs/FileStore/AllInOneParquets/test1')
shutil.move('/dbfs/FileStore/AllInOneParquets/SdId=791221', '/dbfs/FileStore/AllInOneParquets/test2')
Both tries above failed.
os.rename gave me the following exception:
OSError: [Errno 7] Argument list too long
The shutil.move, which tries to use os.rename in the background, started to move the files instead of really renaming.
The 5k files are just a test and I am going for much more than it. Is there a way around?

Try google: python how to move files from one directory to another and you will found the answer :)
How to move a file in Python
and I thing you shod run the code on administrator mode maybe

Related

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__))

FileNotFoundError: [Errno 2]: Reading a file via jupyter

i started using Jupyter and fell into a problem. Whilst trying to read a file called 'vgsales' it gave an error:
FileNotFoundError: [Errno 2] File vgsales.csv does not exist: 'vgsales.csv'
I have put the 'vgsales' folder which has my csv file within it in my desktop with the Jupyter notebook.
Not sure where i went wrong, Please help!
Thanks
Code used:
import pandas as pd
df = pd.read_csv('vgsales.csv')
df
Though the notebook is on your desktop, you still can find out which folder it is in by simply right click on it and choose property. Then you can copy your csv file to the same folder
To list which files are in local folder when you are inside a notebook, use !ls, note that you need ! for the command to work

Python mammoth error: zipfile.BadZipFile: File is not a zip file

My code is ran in Python 3.8.2, name of file is main.py
After I run python main.py, i receive the error like this:
My path to file is : C:\Users\84165\Desktop\KLTN-backend\upload\thainq\Test.docx
I have searched in stackoverflow for hours about this error, but there is no solution.
Am i wrong in somewhere?
Thank you in advance.
There are two reasons this may be happening
Your docx file is completely empty (0 bytes).
The docx file your code is operating on through mammoth.convert_to_html or mammoth.extract_raw_text is currently open. Please close your file, then run the script.

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

Move files with python3

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.

Resources