Creating Directories in Ubuntu with python - python

I am trying to create directory in Ubuntu using python and save my zip files in it. My code is working fine in windows but behaving weirdly with ubuntu.
import os
import zipfile
import datetime
from os.path import expanduser
home = expanduser('~')
zip_folder = home + '\\Documents\\ziprep' # enter folder path where files are
zip_path = home + '\\Documents\\zips' #enter path for zip to be saved
global fantasy_zip
def dailyfiles(weekly_file,today):
today = str(today)
try:
os.mkdir(zip_path + today)
except OSError:
print("Creation of the directory %s failed" % today)
else:
print("Successfully created the directory %s " % today)
for folder, subfolders, files in os.walk(zip_folder):
for file in files:
if file.startswith(today) and not file.endswith('.zip') and file not in weekly_file:
print("Zipping - Filename " + file)
zip_in = zip_path + today + "\\"
fantasy_zip = zipfile.ZipFile(zip_in + file + '.zip', 'w')
fantasy_zip.write(os.path.join(folder, file),
os.path.relpath(os.path.join(folder, file), zip_folder),
compress_type=zipfile.ZIP_DEFLATED)
fantasy_zip.close()
def main():
weekday = str(datetime.datetime.today().weekday())
today = datetime.date.today().strftime('%Y%m%d')
dailyfiles(weekly_file,today)
if __name__ == '__main__':
main()
Logically it should create a folder with todays date at the path specified. But it is creating Folder in ubuntu with the whole path at the same directory where m script is.
For example it is creating folder with name like this: '/home/downloads/scripypath'
Whereas I need '20191106' at the path which is specified in script. The code is working fine in windows.
Link to current project file

in ubuntu directory structure is totally different and they use \ instead of /.
so prepare your link as ubuntu file structure.

I suggest using home + '/Documents/ziprep/'and home + '/Documents/zips/' on lines 8 and 9, respectively.
EDIT: Sorry, forgot why this should solve the problem. In Linux or Unix, directories use "/" instead of "\" (used in Windows) as directory separators.

Related

Python os cannot get path to Desktop on One Drive

I am trying to get user's path to Desktop by using the following code:
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
d = datetime.datetime.today()
newpath = desktop + '\\New_folder' + str(d.day) + '_' + str(d.month) + '_' + str(d.year)
if not os.path.exists(newpath):
os.makedirs(newpath)
print('Desktop folder created: ' + newpath)
For most users that works, but I recently got a case of a user who has everything on One Drive and their path is: 'C:\Users\User1\OneDrive - CompanyName\Desktop'.
For these users, the script fails with this message:
FileNotFoundError: [WinError 3] The system cannot find the file specified 'C:\\Users\\User1\\Desktop\\New_folder_16_3_2020
How do I point python to their actual Desktop path, so that I can then work with that folder?
I suspect the reason for yours is that, make sure that you're accessing the directory through OneDrive instead of following the path to the desktop immediately from the user. So instead of this
C:\\Users\\User1\\Desktop\\New_folder_16_3_2020
it would be something like this
C:\\Users\\User1\\Desktop\\OneDrive\\New_folder_16_3_2020
I would recommend you to add a try/catch statement that if fails prepends/appends the oneDrive keyword to the path.

FileNotFoundError when trying to use os.rename

I've tried to write some code which will rename some files in a folder - essentially, they're listed as xxx_(a).bmp whereas they need to be xxx_a.bmp, where a runs from 1 to 2000.
I've used the inbuilt os.rename function to essentially swap them inside of a loop to get the right numbers, but this gives me FileNotFoundError [WinError2] the system cannot find the file specified Z:/AAA/BBB/xxx_(1).bmp' -> 'Z:/AAA/BBB/xxx_1.bmp'.
I've included the code I've written below if anyone could point me in the right direction. I've checked that I'm working in the right directory and it gives me the directory I'm expecting so I'm not sure why it can't find the files.
import os
n = 2000
folder = r"Z:/AAA/BBB/"
os.chdir(folder)
saved_path = os.getcwd()
print("CWD is" + saved_path)
for i in range(1,n):
old_file = os.path.join(folder, "xxx_(" + str(i) + ").bmp")
new_file = os.path.join(folder, "xxx_" +str(i)+ ".bmp")
os.rename(old_file, new_file)
print('renamed files')
The problem is os.rename doesn't create a new directory if the new name is a filename in a directory that does not currently exist.
In order to create the directory first, you can do the following in Python3:
os.makedirs(dirname, exist_ok=True)
In this case dirname can contain created or not-yet-created subdirectories.
As an alternative, one may use os.renames, which handles new and intermediate directories.
Try iterating files inside the directory and processing the files that meet your criteria.
from pathlib import Path
import re
folder = Path("Z:/AAA/BBB/")
for f in folder.iterdir():
if '(' in f.name:
new_name = f.stem.replace('(', '').replace(')', '')
# using regex
# new_name = re.sub('\(([^)]+)\)', r'\1', f.stem)
extension = f.suffix
new_path = f.with_name(new_name + extension)
f.rename(new_path)

How to get cross platform absolute relative file path?

I'm having issues loading files by thier path over my code which runs over Windows (local test and development) and Linux (CI CD).
While running my code locally in Windows, the file path relative works fine, when my code is running over Linux it turns to a mess and returns an Error: No such file or directory
Is there such a code in Python which is cross platform to solve it ?
My code is like this:
def get_event_json_file_path(fileName):
file_dir = os.path.dirname(os.path.realpath('__file__'))
file_path = os.path.join(file_dir, "events/" + fileName)
return file_path
Is there a code to get the classpath of the folder ?
I managed to code this function:
def get_relative_file_path(file_dir_path, fileName):
dir = os.path.dirname(__file__)
file_path = os.path.join(dir, file_dir_path,fileName)
return file_path
Usage:
get_relative_file_path('../resources/', "restCallBodySchema.json")

Python: Using shutil.move or os.rename to move folders

I have written a script to move video files from one directory to another, it will also search sub directories using os.walk. however if the script finds a video file it will only move the file and not the containing folder. i have added an if statement to check if the containing folder is different to the original search folder.
i cant find the code to actually move(or rename?) the folder and file to a different directory. i have read/watch a lot on moving files and there is a lot of information on that, but i cant find anything for moving folders.
i have tried using shutil.move and os.rename and i get an error both times. when i try and search for the problem i get a lot of results about how to move files, or how to change the current working directory of python.
any advice(even how to phrase the google search to accuratly describe how to find a tutorial on the subject) would be really appreciated. it's my first real world python program and ive learnt a lot but this last step is wearing me down!
EDIT: when trying to use os.rename(src_file, dst_file) i get the error WindowsError: error 3 The system cannot find the path specified.
when trying shutil.move(src_file, dst_file) i get ioerror errno 2 no such file or directory "H:\\Moviesfrom download...\OneOfTheVideoFilesNotInParentFolder ie the folder and the file needs to move.
thanks.
ps like i said it's my first script outside of code academy so any random suggestions would also be appreciated.
import os
import shutil
import time
movietypes = ('.3gp', '.wmv', '.asf', '.avi', '.flv', '.mov', '.mp4', '.ogm', '.mkv',
'. mpg', '.mpg', '.nsc', '.nsv', '.nut', '.a52', '.tta', '.wav', '.ram', '.asf',
'.wmv', '. ogg', '.mka', '.vid', '.lac', '.aac', '.dts', '.tac',
'.dts', '.mbv')
filewrite = open('H:\\Movies from download folder\\Logs\\logstest.txt', 'w')
dir_src = "C:\\Users\\Jeremy\\Downloads\\"
dir_dst = "H:\\Movies from download folder\\"
for root, dirs, files in os.walk(dir_src):
for file in files:
if file.endswith(movietypes) == True:
filestr = str(file)
locationoffoundfile = os.path.realpath(os.path.join(root,filestr))
folderitwasin = locationoffoundfile.replace(dir_src,'')
folderitwasin = folderitwasin.replace(filestr,'')
pathofdir = os.path.realpath(root) + "\\"
if pathofdir != dir_src:
src_file = locationoffoundfile
dst_file = dir_dst + folderitwasin + filestr
os.rename(src_file, dst_file) #****This line is the line im having issues with***
print src_file
print dst_file
filewrite.write(file + " " + "needs to have dir and file moved Moved!" + '\n')
else:
src_file = os.path.join(dir_src, file)
dst_file = os.path.join(dir_dst, file)
print src_file
print dst_file
shutil.move(src_file, dst_file)
filewrite.write(os.path.dirname(file) + '\n')
filewrite.write(file + " " + "needs to have file moved Moved!" + '\n')
filewrite.close()
Looks like you're only moving files, without doing anything about the folders. So if you try to move
C:\Users\Jeremy\Downloads\anime\pokemon.avi
to
H:\Movies from download folder\anime\pokemon.avi
it will fail because there's no anime directory on H:\ yet.
Before iterating through files, iterate through dirs to ensure that the directory exists at your destination, creating it if necessary.
for root, dirs, files in os.walk(dir_src):
for dir in dirs:
dest_dir = os.path.join(dir_dst, dir)
if not os.path.isdir(dest_dir):
os.mkdir(dest_dir)
for file in files:
#rest of code goes here as usual...
As these are MS Windows paths use forward slashes instead and declare path as a string literal; e.g.
dir_dst = r"H:/Movies from download folder/"

Writing to a new directory in Python without changing directory

Currently, I have the following code...
file_name = content.split('=')[1].replace('"', '') #file, gotten previously
fileName = "/" + self.feed + "/" + self.address + "/" + file_name #add folders
output = open(file_name, 'wb')
output.write(url.read())
output.close()
My goal is to have python write the file (under file_name) to a file in the "address" folder in the "feed" folder in the current directory (IE, where the python script is saved)
I've looked into the os module, but I don't want to change my current directory and these directories do not already exist.
First, I'm not 100% confident I understand the question, so let me state my assumption:
1) You want to write to a file in a directory that doesn't exist yet.
2) The path is relative (to the current directory).
3) You don't want to change the current directory.
So, given that:
Check out these two functions: os.makedirs and os.path.join. Since you want to specify a relative path (with respect to the current directory) you don't want to add the initial "/".
dir_path = os.path.join(self.feed, self.address) # will return 'feed/address'
os.makedirs(dir_path) # create directory [current_path]/feed/address
output = open(os.path.join(dir_path, file_name), 'wb')
This will create the file feed/address/file.txt in the same directory as the current script:
import os
file_name = 'file.txt'
script_dir = os.path.dirname(os.path.abspath(__file__))
dest_dir = os.path.join(script_dir, 'feed', 'address')
try:
os.makedirs(dest_dir)
except OSError:
pass # already exists
path = os.path.join(dest_dir, file_name)
with open(path, 'wb') as stream:
stream.write('foo\n')
Commands like os.mkdir don't actually require that you make the folder in your current directory; you can put a relative or absolute path.
os.mkdir('../new_dir')
os.mkdir('/home/you/Desktop/stuff')
I don't know of a way to both recursively create the folders and open the file besides writing such a function yourself - here's approximately the code in-line. os.makedirs will get you most of the way there; using the same mysterious self object you haven't shown us:
dir = "/" + self.feed + "/" + self.address + "/"
os.makedirs(dir)
output = open(os.path.join(dir, file_name), 'wb')

Categories