I want to get all files from Folders.
I restored my hard disk and got a 1000 Folders each with 500 Files, i want to get them out of the foldes into a singel folder so i can run python to sort the files.
Sadly i didn't find anything what works so i hope someone can help me.
So i tryed one thing:
Runing this code in the Windows Console:
pushd C:\Users\KroherL\Downloads
for /r %%a in (*.?*) do (
MOVE "%%a" "C:\Users\KroherL\Music\new%%~nxa"
)
popd
Thank you all in advance.
This works on Windows:
make text file with the following single line
for /r %%i in (*.*) do xcopy /Y "%%i" c:\cumulFolder
(where c:\cumulFolder is the destination folder where you'll find your files all together)
save the text file as .bat (e.g RecurseCopy.bat)
copy this file in the main folder where you want to search and copy files into subfolders too.
doubleclick on your .bat file
Related
hello.py is my first python program. It is saved on my desktop.
In the terminal I write in front of
user#AA-MacBook-Air ~ % python3 hello.py
The error is
can't open file 'hello.py': [Errno 2] No such file or directory
Kindly help me understand the problem and solve it.
In the terminal you are currently in the directory ~. This signifies the folder /Users/<username>. Your script is on your desktop.
Type cd Desktop to change to /Users/<username>/Desktop and then run python3 hello.py.
you first need to change destination with cd
The error message, No such file or directory pretty much gives the explanation. Check if the file hello.py is present in the correct working directory. This can done graphically or using the ls command. If it is not present, copy the file the to the directory or navigate to the location of the file hello.py in terminal using cd.
As mentioned in the coursera help articles in order to download notebooks from the class we need to zip all the content of root folder into single file and download the final workspace.tar.gz using these steps: but it is not working all courses.
Anyone knows proper way to do this !!
Open the home folder of your coursera jupyter notebook:
you can do this by opening any of the course notebooks and thanm selecting file> open or by clicking on Jupyter icon at the top left corner of notebook.
Open terminal inside the notebook:
On the home page of your notebooks, at the top left corner select new> terminal
Check in which dir you are:
this is important as different courses have their materials in different dir!
Some courses have a dir name jovyan and inside that you have two folders generally work and work-ro.
in work you have your actual content that you can see on your notebook home page.
in work-ro you have only read_only folder. This same folder you have it in your work dir but you cant open the content of that folder after downloading! (I dont know why I cant open it)
I turns out that this folder contains images which are in your notebooks. that is the reason you will have to zip both these folders.
Its not necessary that all the course have this folder named work!
In some courses materials are directly inside root dir. In such cases you can find the directory with your material by finding folder name ending with -ro
Ex in one of my course I located a folder named TF-ro and there was another folder named TF containing all course material! As per above pattern TF-ro contained read_only folder.
Just in case you are wondering how to navigate inside terminal: [Use these commands]
ls: list everything inside the folder
cd: to change the folder you are currently in
Ex: cd .. #go to previous folder cd <dirname> #go to that specified folder
compress both the folders using tar:
Navigate to the folder which contains both of these folders i.e work and work-ro or if you read my second case than Tf and TF-ro or folders in your case.
Use this to make tar file:
Use this when your folder contains only two dirs that you want
tar -czvf <choose a name>.tar.gz <address of dir to compress>
Ex: tar -czvf data.tar.gz ./
use this when you are in root folder and you have multiple dir along with the folders you want
tar -czvf <choose a name>.tar.gz <dir1 addres> <dir2 addres>
Ex: tar -czvf data.tar.gz ./work ./work-ro
Just in case you are wondering!
./ means current folder.
Check the size of your tar file:
This is also important!!
If your process of making tar file is taking too long or your terminal appears to be frozen ! than there are some big files in your home folder.
You can check the size of your tar file using: ls -lh data.tar.gz.
Normally the size should not be more than 10 - 15 Mbs.
If your size is in GBs than you are mostly downloading large amount of datasets and csv files!
you cannot download big files like this!
[Workaround for this problem are mentioned below]
run this command: du
This will list all the dir's and the size of dir's in current folder.
Figure out which folder has more size.
Note: size shown in this commands are in Number of sections occupied 1 section = 1024 bytes
Exclude these folder wile making tar...
In order to remove previous tar file run rm data.tar.gz
make the tar like this:
tar -czvf <yourName>.tar.gz --exclude=<address to exclude> <dir/dirs to zip>
Ex: tar -czvf data.tar.gz --exclude=./work/data --exclude=./work/- ./work ./work-ro
Move the file :
You can only see the content in the work folder (or any other folder your content is in) on your class's notebook home folder.
This is why we will move over tar file to that folder.
move using this command mv <file name> <location> Ex :mv data.tar.gz ./work
Download your file:
Now you can see your file in your home folder in your browser. simply select the file you will see download option available at the top !!
Sometimes you dont see the download button on the top, in such cases...
right click your file> save link As> then save it with .tar.gz extension
Just to confirm check the size of file you have downloaded and one in your classroom!!
Work Around for downloading big data sets:
Your course generally does not use all the csv's or data sets that it has stored in the data folder. When you do the assignments see which files are / data sets are used and download only those manually. i.e opening that file on your classroom and downloading it using using file> download
if you still want the entire thing than make separate tar file of that folder only. Than split the tar file (you will find it online easily) and than download as I have mentioned earlier!
After the download it is necessary to concatenate the files:
cat allfiles.tar.gz.part.* > allfiles.tar.gz
I would suggest not to waste time in doing this!! Just download what is required and that's it!!
I hope this was helpful !! cauz I spent 5 hr figuring out how to do it !! ENJOY !!
Alternatively, you could initialize a git repo and push it to your GitHub account.
Open terminal (Jupiter home > new > terminal)
Run the following code: (I'm assuming you've already created a GitHub repo, if not create one and then do the following; you'll need the link to your repo)
git init
git config --global user.name "test"
git config --global user.email "test"
git add -A; git commit -m "commit"
git remote add origin <_your-github-repo-url_>
git push origin master -u --verbose
You can just compress all the programming exercise (notebook + data) by placing this commands at the beginning of your notebook:
import os
!tar chvfz notebook.tar.gz *
print("File size: " + str(os.path.getsize("notebook.tar.gz")/1e6) + " MB")
if os.path.getsize("notebook.tar.gz")/1e6 >100 :
print("Splitting file")
!split -b 100M notebook.tar.gz "notebook.tar.gz."
I have a directory structure with many log files.
Example:
root/feature1/pre/a.log<br>
root/feature1/pre/b.log<br>
root/feature1/post/c.log<br>
root/feature1/post/d.og<br>
root/feature2/pre/e.log<br>
root/feature2/pre/f.log<br>
root/feature2/post/g.log<br>
root/feature2/post/h.log
I want to archive few of the log files with the condition that log files are older than 2 months. I could archive the log files with the given condition but couldn't maintain directory inside tar file.
I need :
root/archive.tar.gz
Where archive.tar.gz contains
feature1/pre/a.log
feature1/post/d.log
feature2/pre/e.log
feature2/post/g.log
feature2/post/h.log
Here, a.log, d.log, e.log, g.log, h.log files are the ones older than 2 months.
This is more of a Linux problem than Python. I suggest using the find command with file name and file time options. Pass those names into the tar command you're using, something like
tar -czf archive `find /root -name feature*.log -ctime +60`
Test this on your command line; when it works as you want, then use Python's os package to execute it from your Python script.
Does that get you moving?
I have some problem with my WIndows CMD.
Some time I need to open python file using CMD command. And I write: 'C:\Program Files\Python X.X\python.exe file.py' but have error: 'C:\Program' isn't system command (maybe not the same, I have another OS language).
With different methods I have different errors but can't open python file.
Examples:
(Picture) translate: can't find 'C:\Program'...
(Picture) another example when I trying to write python directory first and then start python file, but it can't find python file.
Thanks for helping me.
There seems to be 2 different problems here.
Windows does not recognise spaces in directory or file names on the command line, so you need to put the directory insied "" .
i.e. "C:\Program Files\Python 3.4\python.exe"
In your second picture, suggests that run.py does not exist in the current directory. Change Directory to where the run.py file is before running that command.
First of all go to the directory where your python file is located ... like:
cd "c:\users\someone\documents\..."
On your pictures you are trying to run python file located in system32 folder but i guess it is not located there so move where the file is with that cd command
Then as Martin says the problem with path of python.exe is the space between words. To solve put the path into quotation marks.
But u can add python to system path and insted of writing full path u can write only
python file.py
How to add python to path see here https://superuser.com/questions/143119/how-to-add-python-to-the-windows-path
I am trying to copy a single file for eg. C:\test.dll to all the sub folders present in C:\test\ folder, is there any way I can do this Using windows batch script or do I have to use other scripting like python.
for /d /r "c:\test\" %%a in (*) do copy c:\test.dll "%%~fa"
For each folder under c:\test copy the indicated file to the folder
Reading python and modifying it are lots simpler than reading archaic batch commands. Which is probably why you are asking the question in the first place.
import shutil
import os
for dir in os.listdir('c:\\test\\') if os.path.isdir(dir):
shutil.copyfile('c:\\test.dll',dir)
for /R "C:\test\" %a in (*) do copy c:\test.dll "%~fa"