Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Posted by Andy Brown on 16 October 2021
Python makes reading and writing files and looping over files and folders a joys, as this tutorial (which includes a long worked example towards the end) hopefully will demonstrate!
See our full range of Python training resources, or test your knowledge of Python with one of our Python skills assessment tests.
This video has the following accompanying files:
File name | Type | Description |
---|---|---|
Useful links.txt | Text file | Useful web links referenced |
Best films ever.txt | Text file | List of best films to read in |
Read in films.py | Python code | Program to read in list of films |
Write out films.py | Python code | Program to write out worst films |
Looping over files.py | Python code | Program to loop over files and folders |
Listing contents of 3 files.py | Python code | Program to loop over and read in 3 files |
1 - countries 1-10.csv | CSV file | CSV files containing countries - part 1 |
2 - countries 11-20.csv | CSV file | CSV files containing countries - part 2 |
3 - Countries 21-30.csv | CSV file | CSV files containing countries - part 3 |
Click to download a zipped copy of the above files.
After watching this video, you may like to test your understanding by doing one or more of the following exercises:
You can also download the answers to each exercise from the links above.
You can increase the size of your video to make it fill the screen like this:
Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown above.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
To improve the quality of a video, first click on the Settings icon:
Make sure yoiu're playing your video so that the icons shown above appear, then click on this gear icon.
Choose to change the video quality:
Click as above to change your video quality.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):
Don't choose the HD option shown unless your connection speed is fast enough to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
From: | Chandra |
When: | 27 Oct 22 at 14:45 |
# concatenates all excel files in one worksheet
import pandas as pd
import glob
location = r"C:\Users\munichandra.kuppani\Documents\Pandas Experiments\MonthWiseData\*.xls"
excel_files = glob.glob(location)
pd.set_option('display.max_rows', 91)
# pd.set_option('display.max_columns',9)
df1 = pd.DataFrame()
for excel_file in excel_files:
df2 = pd.read_excel(excel_file)
df1 = pd.concat([df1, df2], ignore_index=True)
df1.fillna(value="N/A", inplace=True)
df1.to_excel(r"C:\Users\munichandra.kuppani\Documents\Pandas Experiments\MonthWiseData\combined_excel.xlsx")
print(df1)
i am getting this error, please give me solution :
[Running] python -u "c:\Users\munichandra.kuppani\Documents\Pandas Experiments\all_sleep_data.py"
File "c:\Users\munichandra.kuppani\Documents\Pandas Experiments\all_sleep_data.py", line 6
location = 'C:\Users\munichandra.kuppani\Documents\Pandas Experiments\MonthWiseData\*.xls'
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
[Done] exited with code=1 in 0.468 seconds
From: | Andy B |
When: | 27 Oct 22 at 15:08 |
I don't know the solution, but have left this post up in case anyone else has the time to answer it?
From: | Chandra |
When: | 10 Feb 22 at 11:45 |
Traceback (most recent call last):
File "i:\Wiseowl-Python\File handling.py", line 4, in <module>
File=open(r"C:\Users\Munichandra Kuppani\Downloads\Files needed\Best films ever.text","r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Munichandra Kuppani\\Downloads\\Files needed\\Best films ever.text'
if you don't mind , would you please tell me solution.
From: | Andy B |
When: | 10 Feb 22 at 12:50 |
Are you sure it's not called .txt, rather than .text? Otherwise it's hard to say!
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.