|
5291 | 5291 | { |
5292 | 5292 | "cell_type": "code", |
5293 | 5293 | "execution_count": null, |
5294 | | - "id": "acfd6a4d", |
| 5294 | + "id": "cf38baf4-5486-4c17-bc93-42510f8a6db7", |
5295 | 5295 | "metadata": { |
5296 | 5296 | "editable": true, |
5297 | 5297 | "slideshow": { |
|
5303 | 5303 | "source": [ |
5304 | 5304 | "from pathlib import Path\n", |
5305 | 5305 | "# Create a Path object for the current directory\n", |
5306 | | - "current_directory = Path('.')\n", |
5307 | | - "\n", |
| 5306 | + "current_directory = Path('.')" |
| 5307 | + ] |
| 5308 | + }, |
| 5309 | + { |
| 5310 | + "cell_type": "code", |
| 5311 | + "execution_count": null, |
| 5312 | + "id": "47affbe3-0eb6-4ed6-8717-3ef05b072a80", |
| 5313 | + "metadata": { |
| 5314 | + "editable": true, |
| 5315 | + "slideshow": { |
| 5316 | + "slide_type": "fragment" |
| 5317 | + }, |
| 5318 | + "tags": [] |
| 5319 | + }, |
| 5320 | + "outputs": [], |
| 5321 | + "source": [ |
5308 | 5322 | "# list all ipynb files in the current directory\n", |
5309 | 5323 | "for file in current_directory.glob('*.ipynb'):\n", |
5310 | | - " print(file.name)\n", |
5311 | | - "\n", |
5312 | | - "## We can also list all files in the current directory (commented as the might be long)\n", |
5313 | | - "# for file in current_directory.iterdir():\n", |
5314 | | - "# print(file.name)\n", |
5315 | | - "\n", |
5316 | | - "# Check whether a file exists\n", |
| 5324 | + " print(file.name)" |
| 5325 | + ] |
| 5326 | + }, |
| 5327 | + { |
| 5328 | + "cell_type": "code", |
| 5329 | + "execution_count": null, |
| 5330 | + "id": "98714766-5033-43d8-a9a3-0589b7bf32ec", |
| 5331 | + "metadata": { |
| 5332 | + "editable": true, |
| 5333 | + "slideshow": { |
| 5334 | + "slide_type": "fragment" |
| 5335 | + }, |
| 5336 | + "tags": [] |
| 5337 | + }, |
| 5338 | + "outputs": [], |
| 5339 | + "source": [ |
| 5340 | + "# We can also list all files in the current directory\n", |
| 5341 | + "for file in current_directory.iterdir():\n", |
| 5342 | + " print(file.name)" |
| 5343 | + ] |
| 5344 | + }, |
| 5345 | + { |
| 5346 | + "cell_type": "code", |
| 5347 | + "execution_count": null, |
| 5348 | + "id": "3fe290ad-e12f-4021-b8ef-a6d9bd6a7467", |
| 5349 | + "metadata": { |
| 5350 | + "editable": true, |
| 5351 | + "slideshow": { |
| 5352 | + "slide_type": "fragment" |
| 5353 | + }, |
| 5354 | + "tags": [] |
| 5355 | + }, |
| 5356 | + "outputs": [], |
| 5357 | + "source": [ |
| 5358 | + "# go to subfolders and files in these folders using /\n", |
5317 | 5359 | "file_path = current_directory / 'data' / 'data_file.txt'\n", |
| 5360 | + "\n", |
| 5361 | + "# check whether a file exists\n", |
5318 | 5362 | "if file_path.exists():\n", |
5319 | 5363 | " print(f\"The file {file_path} exists.\")" |
5320 | 5364 | ] |
5321 | 5365 | }, |
5322 | 5366 | { |
5323 | 5367 | "cell_type": "code", |
5324 | 5368 | "execution_count": null, |
5325 | | - "id": "04329a55", |
| 5369 | + "id": "cdcbc0b1-197d-4fc5-95e9-9501868b7525", |
5326 | 5370 | "metadata": { |
5327 | 5371 | "editable": true, |
5328 | 5372 | "slideshow": { |
5329 | | - "slide_type": "" |
| 5373 | + "slide_type": "fragment" |
5330 | 5374 | }, |
5331 | 5375 | "tags": [] |
5332 | 5376 | }, |
5333 | 5377 | "outputs": [], |
5334 | 5378 | "source": [ |
5335 | 5379 | "# Create a new test folder\n", |
5336 | 5380 | "new_folder = current_directory / 'test_folder'\n", |
5337 | | - "new_folder.mkdir(exist_ok=True) # exist_ok=True prevents error if folder already exists\n", |
5338 | | - "\n", |
5339 | | - "# Create a new file in the test folder\n", |
| 5381 | + "new_folder.mkdir(exist_ok=True) # exist_ok=True prevents error if folder already exists" |
| 5382 | + ] |
| 5383 | + }, |
| 5384 | + { |
| 5385 | + "cell_type": "code", |
| 5386 | + "execution_count": null, |
| 5387 | + "id": "b33c4e26-ec82-405d-97a5-bd88488c06d8", |
| 5388 | + "metadata": { |
| 5389 | + "editable": true, |
| 5390 | + "slideshow": { |
| 5391 | + "slide_type": "fragment" |
| 5392 | + }, |
| 5393 | + "tags": [] |
| 5394 | + }, |
| 5395 | + "outputs": [], |
| 5396 | + "source": [ |
| 5397 | + "# Write text to a file in test_folder\n", |
5340 | 5398 | "output = \"This is some output\\nSecond line!\"\n", |
5341 | 5399 | "output_file = new_folder / 'output.txt'\n", |
5342 | | - "output_file.write_text(output)\n", |
5343 | | - "\n", |
| 5400 | + "output_file.write_text(output)" |
| 5401 | + ] |
| 5402 | + }, |
| 5403 | + { |
| 5404 | + "cell_type": "code", |
| 5405 | + "execution_count": null, |
| 5406 | + "id": "89847e34-7e99-4b89-904f-b7ec2c8dd774", |
| 5407 | + "metadata": { |
| 5408 | + "editable": true, |
| 5409 | + "slideshow": { |
| 5410 | + "slide_type": "fragment" |
| 5411 | + }, |
| 5412 | + "tags": [] |
| 5413 | + }, |
| 5414 | + "outputs": [], |
| 5415 | + "source": [ |
5344 | 5416 | "# Read back the content from the file we just created\n", |
5345 | 5417 | "content = output_file.read_text() # Read from the same file we created\n", |
5346 | 5418 | "print(\"Content of the file we just created:\")\n", |
5347 | | - "print(content)\n", |
5348 | | - "\n", |
5349 | | - "# Also demonstrate reading an existing data file\n", |
5350 | | - "data_file = current_directory / 'data' / 'data_file.txt'\n", |
5351 | | - "if data_file.exists():\n", |
5352 | | - " data_content = data_file.read_text()\n", |
5353 | | - " print(f\"\\nContent of {data_file.name}:\")\n", |
5354 | | - " print(data_content)" |
| 5419 | + "print(content)" |
5355 | 5420 | ] |
5356 | 5421 | }, |
5357 | 5422 | { |
|
5398 | 5463 | "source": [ |
5399 | 5464 | "import csv, math\n", |
5400 | 5465 | "\n", |
| 5466 | + "# Read the file object\n", |
5401 | 5467 | "with open(\"example.csv\", 'w') as out_f:\n", |
| 5468 | + " # use csv.writer to interact\n", |
5402 | 5469 | " writer = csv.writer(out_f, delimiter=',')\n", |
| 5470 | + " # write the headings\n", |
5403 | 5471 | " writer.writerow([\"x_axis\", \"y_axis\"])\n", |
| 5472 | + " # generate the data for x\n", |
5404 | 5473 | " x_axis = [x * 0.1 for x in range(0, 100)]\n", |
| 5474 | + " # write x and cos(x) for every entry\n", |
5405 | 5475 | " for x in x_axis:\n", |
5406 | 5476 | " writer.writerow([x, math.cos(x)])\n", |
5407 | 5477 | "\n", |
|
5427 | 5497 | { |
5428 | 5498 | "cell_type": "code", |
5429 | 5499 | "execution_count": null, |
5430 | | - "id": "b4f9954b", |
| 5500 | + "id": "7d2b75d2-f0a2-490e-be7d-28dad71ef05c", |
5431 | 5501 | "metadata": { |
5432 | 5502 | "editable": true, |
5433 | 5503 | "slideshow": { |
|
5440 | 5510 | "# Show the first few lines to demonstrate what we created\n", |
5441 | 5511 | "print(\"First few lines of the file:\")\n", |
5442 | 5512 | "with open(\"example.csv\", 'r') as f:\n", |
5443 | | - " for i, line in enumerate(f):\n", |
5444 | | - " if i < 5: # Show first 5 lines\n", |
5445 | | - " print(f\" {line.strip()}\")\n", |
5446 | | - " else:\n", |
5447 | | - " break\n", |
5448 | | - "\n", |
| 5513 | + " for _ in range(5):\n", |
| 5514 | + " print(f.readline().strip())" |
| 5515 | + ] |
| 5516 | + }, |
| 5517 | + { |
| 5518 | + "cell_type": "code", |
| 5519 | + "execution_count": null, |
| 5520 | + "id": "9af8c87f-62db-4a04-86d8-d51428395210", |
| 5521 | + "metadata": { |
| 5522 | + "editable": true, |
| 5523 | + "slideshow": { |
| 5524 | + "slide_type": "" |
| 5525 | + }, |
| 5526 | + "tags": [] |
| 5527 | + }, |
| 5528 | + "outputs": [], |
| 5529 | + "source": [ |
5449 | 5530 | "# Show the value of Y for X==1.0 saved in the file\n", |
5450 | 5531 | "print(\"The value of Y for X==1.0 saved in the file:\")\n", |
5451 | 5532 | "with open (\"example.csv\", 'r') as in_f:\n", |
|
5782 | 5863 | ], |
5783 | 5864 | "metadata": { |
5784 | 5865 | "kernelspec": { |
5785 | | - "display_name": "teaching_data_analysis", |
| 5866 | + "display_name": "Python 3 (ipykernel)", |
5786 | 5867 | "language": "python", |
5787 | 5868 | "name": "python3" |
5788 | 5869 | }, |
|
0 commit comments