Where does python send files without a directory path? - python-3.x

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

Related

shutil.rmtree does not remove directory

I'm struggling with one error for few hours now. I'm making a setup.exe for my program. Each time I install it I want old dirs and files to be removed and created again. The code I use for that is:
import os
import shutil
data_path = os.path.join(os.environ['HOMEDRIVE'], '\ProgramData', 'MyDir')
shutil.rmtree(data_path, ignore_errors=True)
os.makedirs(data_path)
When I run this I see an error <FileExists> You can not create file that already exists: 'C:\\ProgramData\\MyDir'

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

Rename a file without changing file extension

I have downloaded a csv file(ABC) using python and selenium and need to change the name of the file.
Input= ABC.csv #downloaded file with name ABC
Output= DEF.csv #New file with name DEF.
Any help is appreciated.
try this
import os
os.rename('a.txt', 'b.kml')
From:
How to rename a file using Python
Thanks All.. Used the below code and it is working fine. All others kept throwing the error as File not found. Used complete file path.
os.rename('C:\\Users\\pathname\\ABC.csv', 'C:\\Users\\pathname\\DEF.csv')
You can use os module to rename the file:
import os
os.rename('path to ABC.csv', 'path to DEF.csv')
Explanation:
rename first argument is the original file which needs to be renamed and second argument is the new name which it should be renamed. If the files are in current directory it will work by simply using the file names. Otherwise include the path where these files are there
More information at docs **https://docs.python.org/3/library/os.html#os.rename

Python cant list files in a directory

I follow some articles regarding on how to list files in a directory using the below code :
import os, sys
path = "/python 3.6.4"
dirs = os.list(path)
for file in dirs:
print file
Note: The .py file in which the above code is on the same directory as the path.
Im running it using a command prompt. Any idea would be much appreciated. Thanks!

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